I'm having trouble sending multipart / form-data to the RESTful web service I created. I am trying to upload multimedia files (images, videos, audio) through the RESTful web service. I searched googled to find the best way to do this, and found that POSTing multipart / form-data is the best solution.
The problem is when I send some multipart / form-data data, I get this error message on my Tomcat server:
SEVERE MessageBodyReader not found for media type=multipart/form-data; boundary=----WebKitFormBoundaryTg7uVLcYJ3lsBpQE, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
I tried to find stackoverflow to find the answer, and the problem was that mimepull.jar was missing for many people. I checked that mimepull.jar was in my class, and that is true, so that is not a problem. At this moment I am stuck.
Here are my dependencies in my pom.xml:
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
Here is the corresponding backend code that handles POST messages multipart / form-data:
@POST
@Path("media")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(FormDataMultiPart form ) {
FormDataBodyPart filePart = form.getField("file");
ContentDisposition headerofFilePart = filePart.getContentDisposition();
InputStream uploadedInputStream = filePart.getValueAs(InputStream.class);
String uploadedFileLocation = "C:\\surveymedia\\media" + headerofFilePart.getFileName();
try {
saveFile(uploadedInputStream, uploadedFileLocation);
} catch (Exception e) {
return Response.status(400).entity(e.getCause()).build();
}
String output = "File uploaded to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
Finally, here is the test page that I did to verify that the files were sent to the server:
<html>
<head>
<title></title>
</head>
<body>
<h1>File Upload with Jersey</h1>
<form action="/rest/surveys/media" method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" size="45" />
</p>
<input type="submit" value="Upload It" />
</form>
</body>
</html>
Let me know if you need more information. Thank you in advance!