4
    @GET
        @Path("/{loginId}")
        @Produces(MediaType.APPLICATION_OCTET_STREAM)
        public Response downloadExportedFile(@PathParam("loginId") String loginId) {
                File file = new File("D://abc.txt");
            Response.ResponseBuilder response = Response.ok((Object) file);
            response.header("Content-Disposition", "attachment; filename=newfile.txt");
response.type(MediaType.APPLICATION_OCTET_STREAM_TYPE);
            return response.build();
        }

This gives response as a content of file and not downloading it

Monika
  • 195
  • 1
  • 1
  • 10
  • please refer https://stackoverflow.com/questions/45376911/rest-service-issues-to-download-multiple-zip-files-using-responsebuilder – JavaLearner1 Jul 27 '18 at 07:50
  • I tried same thing here.. but my file is not getting downloaded.. It is just displaying contents of that file in response – Monika Jul 27 '18 at 07:51
  • You should set the content type e.g in header "application/octet-stream" for a response. Browser does not know about it. – Dawid Kubicki Jul 27 '18 at 07:56
  • @d4widk : I tried that also.. still not working – Monika Jul 27 '18 at 08:00
  • Have you tried something like this return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM).build(); – Dawid Kubicki Jul 27 '18 at 08:02
  • in your case I think should works Response.ok((Object) file, MediaType.APPLICATION_OCTET_STREAM); – Dawid Kubicki Jul 27 '18 at 08:04
  • not working... I am using react as my frontend.. do I have to handle something from fronthend.. other than calling it? – Monika Jul 27 '18 at 08:07
  • Have you tested your API without frontend I mean here call API as URL in the browser? :) then you will know if you return proper headers etc and your endpoint works fine, in my opinion, it should work ok as I meant before. – Dawid Kubicki Jul 27 '18 at 08:09
  • because of some configuration issue I cannot test this api without fronend.. but as my all other apis are working with react.. this should also work.. but it is not working – Monika Jul 27 '18 at 08:18
  • I think you should reproduce this code on your computer to see what will happen. The only way to test you API is to write your own Client (a web browser or HttpClient) if needed. – Harry Coder Jul 27 '18 at 13:08

2 Answers2

1

Monika if you use spring I recommend return response entity resource with headers something like this

 @GetMapping("/api/config)
    fun config(@PathVariable id: String): ResponseEntity<Resource> {
        val config = someService.getConfig(hotelId = id)
        val resource InputStreamResource(objectMapper.writeValueAsString(config)
                     .byteInputStream(Charsets.UTF_8))

    val responseHeaders = HttpHeaders()
    responseHeaders.add("content-disposition", 
    "attachment;filename=config.json")
    responseHeaders.add("Content-Type",MediaType.APPLICATION_OCTET_STREAM_VALUE)

    return ResponseEntity.ok()
        .headers(responseHeaders)
        .contentType(MediaType.parseMediaType("application/octet-stream"))
        .body(resource)
}

Here you have some other answer about

Content-Disposition and Content Type

The frontend should not have an impact on downloading file.

0

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.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32