Your code here is the API you are implementing and it returns the content of the file. Downloading your file should be done from a client by generating a new file after you get the content. For instance, with the HttpClient
lib, you will get this code:
CloseableHttpClient client;
HttpGet request;
HttpResponse response;
HttpEntity entity;
try {
client = HttpClientBuilder.create().build();
request = new HttpGet(URI);
response = client.execute(request);
entity = response.getEntity();
// The file not found, or is not available
if(response.getStatusLine().getStatusCode() == 404) {
throw new CustomException("The URI is not valid");
} else {
InputStream is = entity.getContent();
try (FileOutputStream fos = new FileOutputStream(new File(newFilePath))) {
int inByte;
while((inByte = is.read()) != -1) {
fos.write(inByte);
}
}
is.close();
client.close();
}
} catch(IOException e) {
e.printStackTrace();
}
If you want the file to be directly downloaded when you call the URL, you have to give the complete path with the name of the file : http://yourhost/yourfile.txt
, and of course the file should be available on the server. Behind this URL, it is just a href
HTML tag, that will point on your file. In your API, your URL will looks something like this : @Path("/{loginId}/{file}")
, where {file}
stands for the file you want to download.