Reputation: 243
I would like to send an already existing zipped file through my spring controller but I keep getting these error messages org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
or a NoHandlerFoundException
which results in a 404 response. Is there something that I am missing? This is my controller code
@RequestMapping(
method = RequestMethod.GET,
value = BASE + "/download",
produces = "application/zip"
)
@ResponseBody
public void sendZippedFile(HttpServletResponse response) throws IOException {
try{
File file = new File("C:\\Users\\me\\test.zip");
FileInputStream is = new FileInputStream(file);
response.setContentType("application/zip");
response.setHeader("Content-Disposition","inline; filename=" + file.getName());
response.setHeader("Content-Length", String.valueOf(file.length()));
FileCopyUtils.copy(is, response.getOutputStream());
}catch (IOException e){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
Break points in my method are not even being reached.
Upvotes: 2
Views: 2344
Reputation: 7968
You need something like this :
@RequestMapping("/download")
public void download(HttpServletResponse response) throws IOException {
FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\me\\test.zip"));
response.setHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
outputStream.close();
inputStream.close();
}
Upvotes: 2