13

I have the REST Service Code Below code which returns a File ,now the problem is in the response body on the PostMan Client I get a raw response ,how can I can convert it so that it displays the contents of the file to the client ,Goal is to return a file to the user .File Name is "File1.jpeg"

Code:

@RequestMapping(value = URIConstansts.GET_FILE, produces = { "application/json" }, method = RequestMethod.GET)
public @ResponseBody ResponseEntity getFile(@RequestParam(value="fileName", required=false) String fileName,HttpServletRequest request) throws IOException{

    ResponseEntity respEntity = null;

    byte[] reportBytes = null;
    File result=new File("/home/arpit/Documents/PCAP/dummyPath/"+fileName);

    if(result.exists()){
        InputStream inputStream = new FileInputStream("/home/arpit/Documents/PCAP/dummyPath/"+fileName); 


        byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("content-disposition", "attachment; filename=" + fileName);

        respEntity = new ResponseEntity(out, responseHeaders,HttpStatus.OK);


    }else{

        respEntity = new ResponseEntity ("File Not Found", HttpStatus.OK);
    }


    return respEntity;

}
1

2 Answers 2

25

The code below solved my problem:

@RequestMapping(value = URIConstansts.GET_FILE, produces = { "application/json" }, method = RequestMethod.GET)
public @ResponseBody ResponseEntity getFile(@RequestParam(value="fileName", required=false) String fileName,HttpServletRequest request) throws IOException{

    ResponseEntity respEntity = null;

    byte[] reportBytes = null;
    File result=new File("/home/arpit/Documents/PCAP/dummyPath/"+fileName);

    if(result.exists()){
        InputStream inputStream = new FileInputStream("/home/arpit/Documents/PCAP/dummyPath/"+fileName);
        String type=result.toURL().openConnection().guessContentTypeFromName(fileName);

        byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("content-disposition", "attachment; filename=" + fileName);
        responseHeaders.add("Content-Type",type);

        respEntity = new ResponseEntity(out, responseHeaders,HttpStatus.OK);
    }else{
        respEntity = new ResponseEntity ("File Not Found", HttpStatus.OK);
    }
    return respEntity;
}
3
  • 4
    Works perfectly! By the way @ResponseBody annotation is not necessary
    – Dante
    Jan 26, 2017 at 11:16
  • 7
    Note that spring has response header constants org.springframework.http.HttpHeaders.CONTENT_DISPOSITION and org.springframework.http.HttpHeaders.CONTENT_TYPE that may be used instead of string literals.
    – chaserb
    Jul 12, 2017 at 0:29
  • 6
    It's worth mentioning that IOUtils.toByteArray(inputStream) loads the whole file into memory so it isn't the best solution if you try to load a bigger file. Aug 9, 2019 at 6:40
3

You need to use different content type instead of produces = { application/json" }

Content-types

http://silk.nih.gov/public/[email protected]

If it still dont work then try to get HttpServletResponse and write your file data to Stream with response.setContentType();

Note :: Recently I have use response.getOutputStream to write a Excel file. Due to some reasons setting produces wasnt working for me.

Also you can use Firebug in firefox to see Response headers.

2
  • IT worked when I added responseHeaders.setContentType(MediaType.IMAGE_JPEG); but how will I dynamically set my media type as it can be txt,image,xml,js,mov any file Sep 17, 2015 at 23:09
  • 1
    If you know file extensions, you can create a Pre-defiend Map for Extension-CotentType and use it set content type responseHeaders.setContentType(CONTENT_TYPES.get(extension));
    – DhruvG
    Sep 17, 2015 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.