RESTeasy和@MultipartForm,服务器和客户端

遍历jboss的RESTEasy Guide 4.4.0-final。 也请examples来指导。

想要创建一个小型服务,在其中发布带有一些元数据的二进制文件。在这种情况下,我想发布一个文件+“该文件的所有者”。


在我的第一工作中,我使用以下结构(1st-attempt-server)。

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream is,@FormDataParam("file") FormDataContentDisposition fileDetail) {
  Map<String,String> map = fileDetail.getParameters();
}

我能够保存文件,但是从'fileDetail'中可以获得的唯一信息是文件名。 调用“ 1st-attempt-server”的客户端(“ 1st-attempt-client”)如下所示:

public void uploadFile() throws IOException,URISyntaxException {

   String endpoint = "http://localhost:8080/Uploadv03/rest/upload"; 
   String filePath = "/tmp/testimage.jpg";
   File file = new File(filePath);
   FileBody fileBody = new FileBody(file,ContentType.DEFAULT_BINARY);

   multipartentityBuilder builder = multipartentityBuilder.create();
   builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
   builder.addPart("file",fileBody);
   String owner="me-myself";
   builder.addTextBody("owner",owner,ContentType.TEXT_PLAIN);
   HttpEntity entity = builder.build();

   HttpPost request = new HttpPost(endpoint);
   request.setEntity(entity);
   HttpClient client = HttpClientBuilder.create().build();
   try {
        HttpResponse response = client.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statuscode "+statusCode);
      } catch (IOException e) {
          e.printStackTrace();
        }

    }

查看服务器中的地图,该地图只给了我2个密钥对,它们是(名称=文件和文件名= testimage.jpg)-看来我无法获取“所有者”属性。 / p>


在我的第二努力中,我正在使用以下构造(2nd-attempt-server)。我确信几年前我曾经使用过这种构造。

 @POST
 @Path("/load")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response external(@MultipartForm FileUploadForm form) throws IOException {
        logger.info("the 'load'-endpoint ");
 }

支持文件:FileuploadForm看起来像这样(带有getter / setter)

public class FileUploadForm implements Serializable {

    private static final long serialVersionUID = 1L;

    public FileUploadForm() {
    }

    @FormParam("selectedFile")
    @PartType("application/octet-stream")
    private byte[] file;

    @FormParam("owner")
    private String owner;

    getters/setters
}

调用“第二次尝试服务器”的客户端(“第二次尝试客户端”)如下所示:

private void post() throws FileNotFoundException,IOException {

     System.out.println("PostClient2\n");  
     String endpoint = "http://localhost:8080/Uploadv03/rest/upload"; 
     String owner = "me-myself";
     String filename = "naming-the-file";

     String filePath = "/tmp/testimage.jpg"
     File file = new File(filePath);
     FileInputStream inputStream = new FileInputStream(file);

     DefaultHttpClient httpClient = new DefaultHttpClient();
     HttpPost uploadFile = new HttpPost(URL);

     multipartentityBuilder builder= multipartentityBuilder.create();
     builder.addTextBody("owner",ContentType.TEXT_PLAIN);
     builder.addBinaryBody("file",inputStream);
     HttpEntity multipart = builder.build();
     uploadFile.setEntity(multipart);

     HttpResponse response = null;
     try {
          response = httpClient.execute(uploadFile);
        } catch (IOException ex) {
            System.err.println("Unable to make connection");
        }
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("Statuscode "+statusCode);
    }

现在我得到HTTPS status 415作为回应。

第二台服务器,客户端代码缺少什么? 我换成这个似乎没关系

 builder.addBinaryBody("selectedFile",inputStream);

我喜欢这里使用POJO的第二种方法,我正在Tomcat 8中部署代码。当然,这里的球衣代码就足够了,我也看到了其他一些依赖项,但是我希望这种“干净”的方法在可行的情况下工作的示例(在指南中找不到简单的示例)


对于服务器端代码,我正在使用这些jersey-dependencies(属于pom.xml的一部分),并且正在Tomcat 8中提供代码。

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <jersey.version>1.19.4</jersey.version>
  <log4j.version>1.2.17</log4j.version>
</properties>

    <!-- Works for Tomcat 8-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-json -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>${jersey.version}</version>
        </dependency>

          <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-multipart-provider -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
moon_zero 回答:RESTeasy和@MultipartForm,服务器和客户端

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3120910.html

大家都在问