原文: https://howtodoinjava.com/resteasy/jax-rs-resteasy-file-upload-html-form-example/

    在上一篇文章中,我们了解了有关在 JAX-RS RESTEasy 应用中上传文件的信息,其中使用HttpClient库构建了客户端来上传文件。 该客户端是纯 Java 客户端,没有任何 UI 关联。 在这篇文章中,我将构建相同的上传功能,但是这次,我们将有一个与之交互的 UI。

    我们将文件上传到服务器的 UI 如下所示。

    RESTEasy 文件上传 - HTML 表单示例 - 图1

    JAX-RS RESTeasy 文件上传示例

    让我们逐步制作应用:

    1)使用 Maven 创建一个 Eclipse Web 项目进行依赖项管理

    在此处学习操作方法

    2)更新pom.xml中的项目依赖项

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.howtodoinjava</groupId>
    5. <artifactId>RESTfulDemoApplication</artifactId>
    6. <packaging>war</packaging>
    7. <version>1.0-SNAPSHOT</version>
    8. <name>RESTfulDemoApplication Maven Webapp</name>
    9. <url>http://maven.apache.org</url>
    10. <repositories>
    11. <repository>
    12. <id>jboss</id>
    13. <url>http://repository.jboss.org/maven2</url>
    14. </repository>
    15. </repositories>
    16. <dependencies>
    17. <!-- Junit support -->
    18. <dependency>
    19. <groupId>junit</groupId>
    20. <artifactId>junit</artifactId>
    21. <version>3.8.1</version>
    22. <scope>test</scope>
    23. </dependency>
    24. <!-- core library -->
    25. <dependency>
    26. <groupId>org.jboss.resteasy</groupId>
    27. <artifactId>resteasy-jaxrs</artifactId>
    28. <version>2.3.1.GA</version>
    29. </dependency>
    30. <dependency>
    31. <groupId>net.sf.scannotation</groupId>
    32. <artifactId>scannotation</artifactId>
    33. <version>1.0.2</version>
    34. </dependency>
    35. <!-- JAXB provider -->
    36. <dependency>
    37. <groupId>org.jboss.resteasy</groupId>
    38. <artifactId>resteasy-jaxb-provider</artifactId>
    39. <version>2.3.1.GA</version>
    40. </dependency>
    41. <!-- Multipart support -->
    42. <dependency>
    43. <groupId>org.jboss.resteasy</groupId>
    44. <artifactId>resteasy-multipart-provider</artifactId>
    45. <version>2.3.1.GA</version>
    46. </dependency>
    47. <!-- For better I/O control -->
    48. <dependency>
    49. <groupId>commons-io</groupId>
    50. <artifactId>commons-io</artifactId>
    51. <version>2.0.1</version>
    52. </dependency>
    53. </dependencies>
    54. <build>
    55. <finalName>RESTfulDemoApplication</finalName>
    56. </build>
    57. </project>

    3)更新web.xml文件以映射有效的 REST API

    1. <!DOCTYPE web-app PUBLIC
    2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
    4. <web-app>
    5. <display-name>Archetype Created Web Application</display-name>
    6. <!-- Auto scan REST service -->
    7. <context-param>
    8. <param-name>resteasy.scan</param-name>
    9. <param-value>true</param-value>
    10. </context-param>
    11. <listener>
    12. <listener-class>
    13. org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    14. </listener-class>
    15. </listener>
    16. <servlet>
    17. <servlet-name>resteasy-servlet</servlet-name>
    18. <servlet-class>
    19. org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    20. </servlet-class>
    21. </servlet>
    22. <servlet-mapping>
    23. <servlet-name>resteasy-servlet</servlet-name>
    24. <url-pattern>/rest-ws/*</url-pattern>
    25. </servlet-mapping>
    26. </web-app>

    4)使用所需的用户界面更新index.jsp文件

    1. <html>
    2. <body>
    3. <h1>JAX-RS File Upload Example</h1>
    4. <form action="rest-ws/upload-file" method="post" enctype="multipart/form-data">
    5. <p>
    6. File name : <input type="text" name="fileName" />
    7. </p>
    8. <p>
    9. Choose the file : <input type="file" name="selectedFile" />
    10. </p>
    11. <input type="submit" value="Upload" />
    12. </form>
    13. https://www.howtodoinjava.com
    14. </body>
    15. </html>

    5)创建具有映射到 HTML 表单的字段的FileUploadForm

    1. package com.howtodoinjava.client.upload;
    2. import javax.ws.rs.FormParam;
    3. import org.jboss.resteasy.annotations.providers.multipart.PartType;
    4. public class FileUploadForm {
    5. public FileUploadForm() {
    6. }
    7. private byte[] fileData;
    8. private String fileName;
    9. public String getFileName() {
    10. return fileName;
    11. }
    12. @FormParam("fileName")
    13. public void setFileName(String fileName) {
    14. this.fileName = fileName;
    15. }
    16. public byte[] getFileData() {
    17. return fileData;
    18. }
    19. @FormParam("selectedFile")
    20. @PartType("application/octet-stream")
    21. public void setFileData(byte[] fileData) {
    22. this.fileData = fileData;
    23. }
    24. }

    6)创建具有处理上传文件逻辑的 RESTful API

    1. package com.howtodoinjava.client.upload;
    2. import java.io.File;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. import javax.ws.rs.Consumes;
    6. import javax.ws.rs.POST;
    7. import javax.ws.rs.Path;
    8. import javax.ws.rs.core.Response;
    9. import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
    10. @Path("/rest-ws")
    11. public class DemoFileSaver_MultipartForm
    12. {
    13. @POST
    14. @Path("/upload-file")
    15. @Consumes("multipart/form-data")
    16. public Response uploadFile(@MultipartForm FileUploadForm form) {
    17. String fileName = form.getFileName() == null ? "Unknown" : form.getFileName() ;
    18. String completeFilePath = "c:/temp/" + fileName;
    19. try
    20. {
    21. //Save the file
    22. File file = new File(completeFilePath);
    23. if (!file.exists())
    24. {
    25. file.createNewFile();
    26. }
    27. FileOutputStream fos = new FileOutputStream(file);
    28. fos.write(form.getFileData());
    29. fos.flush();
    30. fos.close();
    31. }
    32. catch (IOException e)
    33. {
    34. e.printStackTrace();
    35. }
    36. //Build a response to return
    37. return Response.status(200)
    38. .entity("uploadFile is called, Uploaded file name : " + fileName).build();
    39. }
    40. }

    7)测试应用

    祝您学习愉快!