使用Spring Boot和Thymeleaf创建文件下载链接

shy*_*yam 4 java download thymeleaf spring-boot

这可能听起来像一个微不足道的问题,但经过几个小时的搜索,我还没有找到答案.据我所知,问题是我试图FileSystemResource从控制器返回一个,而Thymeleaf希望我提供一个String资源,用它来渲染下一页.但是因为我回来了FileSystemResource,我得到以下错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "products/download", template might not exist or might not be accessible by any of the configured Template Resolvers
Run Code Online (Sandbox Code Playgroud)

我使用的控制器映射是:

@RequestMapping(value="/products/download", method=RequestMethod.GET)
public FileSystemResource downloadFile(@Param(value="id") Long id) {
    Product product = productRepo.findOne(id);
    return new FileSystemResource(new File(product.getFileUrl()));
}
Run Code Online (Sandbox Code Playgroud)

我的HTML链接看起来像这样:

<a th:href="${'products/download?id=' + product.id}"><span th:text="${product.name}"></span></a>
Run Code Online (Sandbox Code Playgroud)

我不希望被重定向到任何地方,我只需要在点击链接后下载文件.这实际上是正确的实现吗?我不确定.

Lea*_*edo 9

您需要将其更改为th:href:

<a th:href="@{|/products/download?id=${product.id}|}"><span th:text="${product.name}"></span></a>
Run Code Online (Sandbox Code Playgroud)

然后您还需要更改控制器并包含@ResponseBody注释:

@RequestMapping(value="/products/download", method=RequestMethod.GET)
@ResponseBody
public FileSystemResource downloadFile(@Param(value="id") Long id) {
    Product product = productRepo.findOne(id);
    return new FileSystemResource(new File(product.getFileUrl()));
}
Run Code Online (Sandbox Code Playgroud)

  • 这只是连接的另一种方式,但比使用'更容易.请检查以下答案:http://stackoverflow.com/a/22064656/4316870 (3认同)

Pad*_*elu 5

有一个 href 网址,例如

<a th:href="@{|/download?id=${obj.id}|}">download</a>
Run Code Online (Sandbox Code Playgroud)

然后在控制器中定义如下,将文件写入响应对象。

@RequestMapping(value="/download", method=RequestMethod.GET)
public void downloadFile(@Param(value="id") Long id,HttpServletResponse response) {
        try {
            Ticket ticket = this.findById(id);
        String fileName =  Paths.get(property.getFileLocation()).resolve(ticket.getFilePath()).toString();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", fileName);
        response.setHeader(headerKey, headerValue);
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream(fileName);
            try {
                int c;
                while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
                }
            } finally {
                if (inputStream != null)
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    response.getWriter().close();
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
Run Code Online (Sandbox Code Playgroud)