简体   繁体   中英

Spring MVC file download IOUtils.copy works but not FileCopyUtils

I'm running into a range issue. I'm trying to have a Spring controller that allow use to download a zip file. Here is the snippet that works, when the user make a get request to the url the browser start the file download

@RequestMapping(value = "mypath/",method = RequestMethod.GET)
  public void downloadFiles(@PathVariable("id") String id) {

    InputStream a = new ByteArrayInputStream(fileService.get(id));
    StringBuilder sb = new StringBuilder("attachment; filename=).append(id).append(".zip");
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, sb.toString());
    org.apache.commons.io.IOUtils.copy(a, response.getOutputStream());
    response.setContentType("application/x-download");
    response.flushBuffer();
   }

But... if I replace IOUtils.copy by FileCopyUtils.copy , when I hit the url the browser simply display the content of file instead of downloading it

Could some explain me what is happening ?

old question but might be helpful for future or current searchers.

I am currently working on this and i have used both for downloading file wih Spring MVC4 and MongoDB and they both work well.

response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=\""+ mDBGridFSFile.getFilename() +"\"");

here mDBGridFSFile is MongoDB GridFS File for containing file content.

This is the contentType i am using for forcing it to download instead of dumping the buffer on the browser and forcing it to write it to disk.

//FileCopyUtils.copy(mDBGridFSFile.getInputStream(), response.getOutputStream());

IOUtils.copyLarge(mDBGridFSFile.getInputStream(), response.getOutputStream());

I have tested both in my project it works, there might be issue with your header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM