java - 如何在Spring mvc和mongodb中下载文件

标签 java spring mongodb spring-mvc

您好,已经编写了如下所示的 Controller 类。我正在尝试从 mongo 数据库获取文件并尝试下载它。

    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 提供了各种内置的资源处理程序。

http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/html/resources.html#resources-implementations

如果您的方法返回其中之一(在您的情况下可能是 ByteArrayResource),那么您只需要在接口(interface)上添加一些注释,如下所示:

@RequestMapping(value = "/foo/bar/{fileId}", 
    method = RequestMethod.GET, 
    produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
@ResponseBody FileSystemResource downloadFile(Long fileId);

不会以这种方式为您摆弄编码和 header 。我建议您在自己动手之前尝试一下。

编辑:以上在 Spring 3.1.4 中运行良好。它不再适用于 3.2.x 或 4.x。以前,produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE } 会导致 Spring 添加适当的 header ,现在它将其视为限制。如果使用标准网络浏览器访问 URL,则不会发送“application/octet-stream”的接受 header 。因此,Spring 将返回 406 错误。为了让它再次工作,需要在没有“produces”属性的情况下重写这样的方法。相反,将 HttpServletResponse 添加到方法参数并在方法内添加 header 。即:

@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 运行。所以目前的解决方案似乎是同时进行上述两种操作!

关于java - 如何在Spring mvc和mongodb中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19878226/

相关文章:

java - 关闭 CipherInputStream 的替代方法

java - Quarkus,无法使用 slf4j 登录单元测试

java - 更大的滚动条

json - 使用 Mean Stack 中的 REST API 更新 JSON 中的子数组

node.js - 引用 Mongoose 模式的类型错误

java - 在Kotlin中:从两个 map 中求和一个公钥的两个值的一种优雅方法是什么?

java - 外部化 Spring 库 Web 应用程序

java - Spring Bean 属性文件

java - Spring 框架: How can i start a web project with eclipse j2ee indigo?

mongodb - Windows 上最好的 MongoDB 托管服务提供商是什么?