无法下载文件。浏览器打开它。
Can't download file. Browser opens it instead.
我已经尝试了很多 contentTypes 和 headers 我已经看到这里但仍然无法弄清楚我做错了什么。我有以下 Spring 控制器:
@RequestMapping(value = "/anexo/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getAnexoById(@PathVariable int id, HttpServletResponse response) {
Anexo a = anexoDAO.getAnexo(id);
if (a == null)
return new ResponseEntity<String>(HttpStatusMessage.NOT_FOUND, HttpStatus.NOT_FOUND);
else {
try {
File dir = new File("temp");
if (!dir.exists())
dir.mkdirs();
String filePath = dir.getAbsolutePath() + File.separator + a.getName();
File serverFile = new File(filePath);
FileInputStream fistream = new FileInputStream(serverFile);
org.apache.commons.io.IOUtils.copy(fistream, response.getOutputStream());
response.setHeader("Content-Disposition", "attachment; filename=" + a.getName());
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(serverFile.length()));
response.setHeader("Content-Transfer-Encoding", "binary");
response.flushBuffer();
System.out.println(response.toString());
return new ResponseEntity<String>(HttpStatus.OK);
} catch (IOException ex) {
return new ResponseEntity<String>("Exception on getting file", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
我也尝试过使用和不使用 @ResponseBody
。
用户将能够上传任何类型的文件到服务器,然后他将能够通过这个控制器下载。问题是浏览器没有下载 window,而是在页面中打开文件。我怎样才能下载它?
提前致谢
这对我有用:
@ResponseBody
void getOne(@PathVariable("id") long id, HttpServletResponse response) throws IOException {
MyFile file = fileRepository.findOne(id);
if(file == null) throw new ResourceNotFoundException();
response.setContentType(file.getContentType());
response.setHeader("Content-Disposition", "attachment; filename=\""+ file.getName() +"\"");
response.setContentLength(file.getData().length);
FileCopyUtils.copy(file.getData(), response.getOutputStream());
}
MyFile 是这样的 class:
class MyFile {
private Long id;
private String contentType;
private String name;
private bit[] data;
...
}
我已经尝试了很多 contentTypes 和 headers 我已经看到这里但仍然无法弄清楚我做错了什么。我有以下 Spring 控制器:
@RequestMapping(value = "/anexo/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> getAnexoById(@PathVariable int id, HttpServletResponse response) {
Anexo a = anexoDAO.getAnexo(id);
if (a == null)
return new ResponseEntity<String>(HttpStatusMessage.NOT_FOUND, HttpStatus.NOT_FOUND);
else {
try {
File dir = new File("temp");
if (!dir.exists())
dir.mkdirs();
String filePath = dir.getAbsolutePath() + File.separator + a.getName();
File serverFile = new File(filePath);
FileInputStream fistream = new FileInputStream(serverFile);
org.apache.commons.io.IOUtils.copy(fistream, response.getOutputStream());
response.setHeader("Content-Disposition", "attachment; filename=" + a.getName());
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(serverFile.length()));
response.setHeader("Content-Transfer-Encoding", "binary");
response.flushBuffer();
System.out.println(response.toString());
return new ResponseEntity<String>(HttpStatus.OK);
} catch (IOException ex) {
return new ResponseEntity<String>("Exception on getting file", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
我也尝试过使用和不使用 @ResponseBody
。
用户将能够上传任何类型的文件到服务器,然后他将能够通过这个控制器下载。问题是浏览器没有下载 window,而是在页面中打开文件。我怎样才能下载它?
提前致谢
这对我有用:
@ResponseBody
void getOne(@PathVariable("id") long id, HttpServletResponse response) throws IOException {
MyFile file = fileRepository.findOne(id);
if(file == null) throw new ResourceNotFoundException();
response.setContentType(file.getContentType());
response.setHeader("Content-Disposition", "attachment; filename=\""+ file.getName() +"\"");
response.setContentLength(file.getData().length);
FileCopyUtils.copy(file.getData(), response.getOutputStream());
}
MyFile 是这样的 class:
class MyFile {
private Long id;
private String contentType;
private String name;
private bit[] data;
...
}