簡體   English   中英

使用 Spring Boot 和 Thymeleaf 創建文件下載鏈接

[英]Creating a file download link using Spring Boot and Thymeleaf

這聽起來像是一個微不足道的問題,但經過數小時的搜索,我還沒有找到答案。 據我所知,問題在於我試圖從控制器返回一個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

我使用的控制器映射是:

@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()));
}

我的 HTML 鏈接如下所示:

<a th:href="${'products/download?id=' + product.id}"><span th:text="${product.name}"></span></a>

我不想被重定向到任何地方,我只需要在單擊鏈接后下載文件。 這實際上是正確的實現嗎? 我不知道。

您需要將th:href更改為:

<a th:href="@{|/products/download?id=${product.id}|}"><span th:text="${product.name}"></span></a>

然后,您還需要更改控制器並包含@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()));
}

有一個href url

<a th:href="@{|/download?id=${obj.id}|}">download</a>

然后在控制器中定義如下將文件寫入響應對象。

@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();

        }
    }

Thymeleaf 有很好的文檔記錄,您可以在手冊中找到文字替換(Leandro 上面展示的內容) - 第 4.7 節

摘自手冊

<span th:text="|Welcome to our application, ${user.name}!|">

對我來說,@leandro-carracedo 的答案有效,但瀏覽器只會列出文件內容,而不是下載。

這修復了:

<a download="results.csv" th:href=... </a>

Thymeleaf <td><a th:href="${fileDetail.MsgFileName}" th:text="${fileDetail.MsgFileName}" /></td> .URL 在服務器端准備好,在客戶端創建 href

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM