<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # RESTEasy - 文件下載示例 > 原文: [https://howtodoinjava.com/resteasy/resteasy-file-download-example/](https://howtodoinjava.com/resteasy/resteasy-file-download-example/) [**RESTEasy**](http://resteasy.jboss.org/ "resteasy") 是 JBOSS 提供的 [**JAX-RS 規范**](https://jcp.org/en/jsr/detail?id=311 "jax-rs")的實現,用于構建 [**RESTful**](https://en.wikipedia.org/wiki/Representational_state_transfer "restful") Web 服務和 RESTful Java 應用。 RESTEasy 與 HTTP 媒體類型結合使用,以特定格式(例如圖像,pdf 或文本)提供響應。 為響應而支持多種媒體類型的配置的核心組件是[**`@Produces`**](https://docs.oracle.com/javaee/6/api/javax/ws/rs/Produces.html "Produces annotation")注解。 可以在此[**鏈接**](https://en.wikipedia.org/wiki/Internet_media_type "media types")中找到此類媒體類型的完整列表。 出于演示目的,我將展示下載一個圖像,一個文本和一個 pdf 文件的示例。 同樣,您可以構建其他媒體類型(文件類型)。 ## 1)創建一個 Maven 項目 ```java mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false mvn eclipse:eclipse -Dwtpversion=2.0 ``` ## 2)更新`pom.xml`文件中的 jax-rs 依賴項 ```java &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;com.howtodoinjava&lt;/groupId&gt; &lt;artifactId&gt;RESTfulDemoApplication&lt;/artifactId&gt; &lt;packaging&gt;war&lt;/packaging&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;name&gt;RESTfulDemoApplication Maven Webapp&lt;/name&gt; &lt;url&gt;http://maven.apache.org&lt;/url&gt; &lt;repositories&gt; &lt;repository&gt; &lt;id&gt;jboss&lt;/id&gt; &lt;url&gt;http://repository.jboss.org/maven2&lt;/url&gt; &lt;/repository&gt; &lt;/repositories&gt; &lt;dependencies&gt; &lt;!-- core library --&gt; &lt;dependency&gt; &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt; &lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt; &lt;version&gt;2.3.1.GA&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;net.sf.scannotation&lt;/groupId&gt; &lt;artifactId&gt;scannotation&lt;/artifactId&gt; &lt;version&gt;1.0.2&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;finalName&gt;RESTfulDemoApplication&lt;/finalName&gt; &lt;/build&gt; &lt;/project&gt; ``` ## 3)在`@Produces`注解中創建具有相應媒體類型的服務類和 API ```java package com.howtodoinjava.service; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; @Path(&quot;/file-management&quot;) public class FileServer { @GET @Path(&quot;/{fileName}/text&quot;) @Produces(&quot;text/plain&quot;) public Response getFileInTextFormat(@PathParam(&quot;fileName&quot;) String fileName) { System.out.println(&quot;File requested is : &quot; + fileName); //Put some validations here such as invalid file name or missing file name if(fileName == null || fileName.isEmpty()) { ResponseBuilder response = Response.status(Status.BAD_REQUEST); return response.build(); } //Prepare a file object with file to return File file = new File(&quot;c:/demoTxtFile.txt&quot;); ResponseBuilder response = Response.ok((Object) file); response.header(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;howtodoinjava.txt&quot;&quot;); return response.build(); } @GET @Path(&quot;/{fileName}/pdf&quot;) @Produces(&quot;application/pdf&quot;) public Response getFileInPDFFormat(@PathParam(&quot;fileName&quot;) String fileName) { System.out.println(&quot;File requested is : &quot; + fileName); //Put some validations here such as invalid file name or missing file name if(fileName == null || fileName.isEmpty()) { ResponseBuilder response = Response.status(Status.BAD_REQUEST); return response.build(); } //Prepare a file object with file to return File file = new File(&quot;c:/demoPDFFile.pdf&quot;); ResponseBuilder response = Response.ok((Object) file); response.header(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;howtodoinjava.pdf&quot;&quot;); return response.build(); } @GET @Path(&quot;/{fileName}/image&quot;) @Produces(&quot;image/jpeg&quot;) public Response getFileInJPEGFormat(@PathParam(&quot;fileName&quot;) String fileName) { System.out.println(&quot;File requested is : &quot; + fileName); //Put some validations here such as invalid file name or missing file name if(fileName == null || fileName.isEmpty()) { ResponseBuilder response = Response.status(Status.BAD_REQUEST); return response.build(); } //Prepare a file object with file to return File file = new File(&quot;c:/demoJpegFile.jpeg&quot;); ResponseBuilder response = Response.ok((Object) file); response.header(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;howtodoinjava.jpeg&quot;&quot;); return response.build(); } } ``` ## 4)將要下載的文件放在參考位置 我正在引用“`c:/<文件>`”中的文件。 在運行此示例之前,從下載鏈接中的分發文件夾復制文件,并將其粘貼到`c:`中。 ## 5)測試應用 以下是在瀏覽器中顯示的用于請求 URL 的下載窗口的快照: **`http://localhost:8080/RESTfulDemoApplication/file-management/demoTxtFile/text`** ![download_txt](https://img.kancloud.cn/27/12/2712815a51daba5ea55d72f42471371d_442x313.png) **`http://localhost:8080/RESTfulDemoApplication/file-management/demoPDFFile/pdf`** ![download_pdf](https://img.kancloud.cn/25/dd/25dd026b3ffb0eb826c4fe17daeb51f9_443x315.png) **`http://localhost:8080/RESTfulDemoApplication/file-management/demoJpegFile/image`** ![download_image](https://img.kancloud.cn/1b/91/1b9160cb2ce2a44b7e8d1bcb7b0a2049_433x312.png) 如果要下載以上示例的源代碼,請遵循以下給定的鏈接。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看