3

I creating a REST service which will send large Files such as ISO image,but currently I am getting out of memory error,Below is my 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/XXX/XXX/XXX/dummyPath/"+fileName);

    if(result.exists()){
        InputStream inputStream = new FileInputStream("/home/XXX/XXX/XXX/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;

}
arpit joshi
  • 1,987
  • 8
  • 36
  • 62
  • What is the size of the file? – Roman C Oct 03 '15 at 11:24
  • 2
    You can avoid reading the entire file into memory at once: http://stackoverflow.com/questions/15800565/spring-mvc-large-files-for-download-outofmemoryexception – approxiblue Oct 03 '15 at 16:07
  • @approxiblue yes the post helped but one question for the large to be send we define the bytes to be downloaded,what would be the ideal ,as this file is of 700 to 900 MB – arpit joshi Oct 04 '15 at 04:41

1 Answers1

3

It looks like you need to use InputStreamResource for returning stream instead of whole file at once.


    package app.controller;

    import org.springframework.core.io.InputStreamResource;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    @org.springframework.web.bind.annotation.RestController
    @RequestMapping(path = {"/rest"})
    public class RestController {

        @RequestMapping(value = "/getISO", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        public ResponseEntity getIsoFile(@RequestParam(value="filePath", required = true) String filePath) throws FileNotFoundException {
            File file = new File(filePath);
            FileInputStream inputStream = new FileInputStream(file);
            InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentLength((int)file.length());
            return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
        }
    }

Also notice, you should use in this case MediaType.APPLICATION_OCTET_STREAM_VALUE instead of application/json as produced type.

Hope this helps.

Konstantin Konyshev
  • 1,026
  • 9
  • 18