RESTeasy and the @MultipartForm , server and client

大憨熊 提交于 2019-12-24 22:28:34

问题


Walking through the RESTEasy Guide 4.4.0-final from jboss. Having the examples to guide be as well.

Wanting to create a small service where you post a binary-file with some metadata. In this particular case I would like to post a file + 'the owner of that file'.


In my first effort I am using the following construct (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();
}

I am able to save the file, but the only information I can get from 'fileDetail' is the filename. The client ('1st-attempt-client') calling '1st-attempt-server' looks like this:

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

    }

Looking into the map in the server, that map only gives me 2-key-pairs and those are (name=file and filename=testimage.jpg) -it seems that I cannot fetch the 'owner'-attribute.


In my second effort I am using the following construct (2nd-attempt-server). I am sure that I have used this construct before, couple of years ago.

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

the supporting file:FileuploadForm looks like this (with getters/setters)

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
}

The client ('2nd-attempt-client') calling '2nd-attempt-server' looks like this:

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", 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);
    }

Now I am getting HTTPS status 415 as a response.

The second server, what is lacking in the client-side code? It does not seem to matter if I change to this

 builder.addBinaryBody("selectedFile", inputStream);

I do like the second approach here using a POJO, I am deploying the code in Tomcat 8. Surely the jersey-code would be sufficient here, I have seen some other dependencies as well, but I would like this 'clean' example to work if that is feasible ( I do not find a simple example in the guide)


For the server-side code I am using these jersey-dependencies (part of my pom.xml) and I am deplying the code in 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>

来源:https://stackoverflow.com/questions/58809689/resteasy-and-the-multipartform-server-and-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!