<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring MVC 下載文件控制器示例 > 原文: [https://howtodoinjava.com/spring-mvc/spring-mvc-download-file-controller-example/](https://howtodoinjava.com/spring-mvc/spring-mvc-download-file-controller-example/) 在 [Spring MVC](https://howtodoinjava.com/spring/spring-mvc/spring-mvc-hello-world-example/) 應用程序中,要將文件之類的資源下載到瀏覽器,您需要在控制器中執行以下操作。 1. 將`void`返回類型用于您的請求處理方法,然后將`HttpServletResponse`添加為該方法的參數。 2. 將響應的[內容類型](https://www.iana.org/assignments/media-types/media-types.xhtml)設置為文件的內容類型。 如果您不知道內容類型是什么,或者希望瀏覽器始終顯示`Save As`對話框,請將其設置為`APPLICATION/OCTET-STREAM` (不區分大小寫)。 3. 添加一個名為`Content-Disposition`的 HTTP 響應標頭,并為其賦予值`attachment; filename=fileName`,其中`fileName`是應出現在“文件下載”對話框中的默認文件名。 ## 1\. Sping MVC 文件下載控制器 讓我們看一下文件下載控制器的示例實現。 ```java @Controller @RequestMapping("/download") public class FileDownloadController { @RequestMapping("/pdf/{fileName:.+}") public void downloadPDFResource( HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName) { //If user is not authorized - he should be thrown out from here itself //Authorized user will download the file String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/pdf/"); Path file = Paths.get(dataDirectory, fileName); if (Files.exists(file)) { response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename="+fileName); try { Files.copy(file, response.getOutputStream()); response.getOutputStream().flush(); } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 如果您只想為授權用戶啟用下載功能,請先在該方法中檢查用戶的登錄狀態,然后再允許下載,否則將其重定向到登錄屏幕。 現在,如果您點擊應用程序 URL:`http://localhost:8080/springmvcexample/download/pdf/sample.pdf`,您將能夠在瀏覽器中獲得**另存為**對話框,如下所示: ![File download window](https://img.kancloud.cn/dd/f5/ddf53606dd497a51169b37b27912c070_447x340.jpg) 文件下載窗口 該文件位于文件夾`/WEB-INF/downloads/pdf`中。 您可以自由更改路徑 – 確保同時更改控制器代碼。 ![File download project structure](https://img.kancloud.cn/72/b2/72b2d99cf5fa3638e08936fd585e67bf_319x340.jpg) 文件下載項目結構 ## 2\. 防止文件下載的交叉引用 很多時候,其他網站可能會[作為直接鏈接交叉引用](https://en.wikipedia.org/wiki/Cross-reference)您網站中的文件。 您可能不想允許它。 要禁止來自其他域的所有下載請求,您可以檢查`referer`標頭中是否包含您的域名。 僅當`referer`標頭不為`null`時,我們修改后的`FileDownloadController`才會將文件發送到瀏覽器。 這將防止通過在瀏覽器中鍵入圖像的 URL 或來自其他域的請求來直接下載圖像。 ```java @Controller @RequestMapping("/download") public class FileDownloadController { @RequestMapping("/pdf/{fileName:.+}") public void downloadPDFResource( HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName, @RequestHeader String referer) { //Check the renderer if(referer != null && !referer.isEmpty()) { //do nothing //or send error } //If user is not authorized - he should be thrown out from here itself //Authorized user will download the file String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/pdf/"); Path file = Paths.get(dataDirectory, fileName); if (Files.exists(file)) { response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename="+fileName); try { Files.copy(file, response.getOutputStream()); response.getOutputStream().flush(); } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 現在,如果您嘗試直接從瀏覽器中訪問 URL,則會收到此錯誤: ```java java.lang.IllegalStateException: Missing header 'referer' of type [java.lang.String] at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.raiseMissingHeaderException(HandlerMethodInvoker.java:797) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestHeader(HandlerMethodInvoker.java:566) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:355) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) ``` 將我的問題放在評論部分。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看