[英]How to download file in Spring mvc and mongodb
嗨,我写了如下的控制器类。 我正在尝试从mongo db获取文件并尝试下载它。
organizationFileAttachmentService.setUser(getUser());
GridFSDBFile file = organizationFileAttachmentService.getGridFSDBFileById(new ObjectId(id), "File");
if (file != null) {
byte[] content = organizationFileAttachmentService.findByIdAndBucket(new ObjectId(id), "File");
try {
int size = content.length;
InputStream is = null;
byte[] b = new byte[size];
try {
is = new ByteArrayInputStream(content);
is.read(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception ex) {
}
}
response.setContentType(file.getContentType());
// String attachment =
// "attachment; filename=\""+file.getFilename()+"\"";
String attachment = "attachment; filename=" + file.getFilename();
// response.setContentLength(new
// Long(file.getLength()).intValue());
response.setCharacterEncoding(file.getMD5());
response.setHeader("content-Disposition", attachment);// "attachment;filename=test.xls"
// copy it to response's OutputStream
// FileCopyUtils.copy(is, response.getOutputStream());
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
} catch (IOException ex) {
_logger.info("Error writing file to output stream. Filename was '" + id + "'");
throw new RuntimeException("IOError writing file to output stream");
}
但我无法下载文件。 谁能帮我。
万一您错过了它,Spring提供了各种内置的资源处理程序。
如果您的方法返回其中一个(也许是ByteArrayResource),那么您只需要在接口上添加几个注释,如下所示:
@RequestMapping(value = "/foo/bar/{fileId}",
method = RequestMethod.GET,
produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
@ResponseBody FileSystemResource downloadFile(Long fileId);
这样就不必为编码和标头摆弄了。 我建议您在自己动手之前尝试一下。
编辑:上面的工作在Spring 3.1.4中很好。 它不再适用于3.2.x或4.x。 以前,produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE}会导致Spring添加适当的标头,但现在将其视为限制。 如果使用标准的Web浏览器访问URL,则不会发送“ application / octet-stream”的接受标头。 因此,Spring将返回406错误。 为了使它再次工作,需要重写这种方法而没有“ produces”属性。 而是将HttpServletResponse添加到方法参数中,并将标头添加到方法内部。 即:
@RequestMapping(value = "/foo/bar/{fileId}",
method = RequestMethod.GET)
@ResponseBody FileSystemResource downloadFile(
Long fileId, HttpServletResponse response) {
...
response.setHeader( "Content-Disposition", "attachment;filename=" + fileName );
...
}
编辑redux:现在通过Spring Boot 1.1.8使用Spring 4.0.7。 似乎设置了produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE }
指令现在又可以工作了。 对于所有我尝试过的浏览器来说,仅具有该指令就足够了。 但是请注意,我还发现它没有设置Content-Disposition
,它保留为application/json
。 尽管对于浏览器来说这似乎不是问题,但我遇到了PHP客户端应用程序中的错误,这些错误似乎仅基于Content-Disposition
。 因此,当前的解决方案似乎是同时完成以上两项!
我已将请求更改为GET,并在html的锚标记中添加了请求。 Aslo将我的代码更改为
@RequestMapping(value = "/getFileById/{id}", method = RequestMethod.GET)
public @ResponseBody
void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
organizationFileAttachmentService.setUser(getUser());
GridFSDBFile file = organizationFileAttachmentService.getGridFSDBFileById(new ObjectId(id), "File");
if (file != null) {
try {
response.setContentType(file.getContentType());
response.setContentLength((new Long(file.getLength()).intValue()));
response.setHeader("content-Disposition", "attachment; filename=" + file.getFilename());// "attachment;filename=test.xls"
// copy it to response's OutputStream
IOUtils.copyLarge(file.getInputStream(), response.getOutputStream());
} catch (IOException ex) {
_logger.info("Error writing file to output stream. Filename was '" + id + "'");
throw new RuntimeException("IOError writing file to output stream");
}
}
}
现在对我来说很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.