When uploading a file using test.html, I get a HTTP 415 error, unsupported media type. It appears something is not working correctly with multipart in jersey. In the console of my server, I get the following error:
No message body reader has been found for class org.glassfish.jersey.media.multipart.FormDataContentDisposition,
ContentType: multipart/form-data;boundary=----WebKitFormBoundaryFwksqvOnuCUBmh87
After doing some research on the web and other StackOverflow articles, I couldn't find an appropriate solution. One of the recurring themes I've seen is about registering the Jersey multipart feature, but I'm not clear on where or how to do that within my file structure. I think I'm close to a working solution, but not there yet. Here's my code currently:
Project Structure
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.box</groupId>
<artifactId>box-java-sdk</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
UploadFiles.java
package wasdev.sample.rest;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
@ApplicationPath("api")
@Path("/upload")
public class UploadFiles{
@POST
@Path("/")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
String filename = fileMetaData.getFileName();
String UPLOAD_PATH = "\\home\\andy" + filename;
try
{
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out = new FileOutputStream(new File(UPLOAD_PATH));
while ((read = fileInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e)
{
throw new WebApplicationException("Error while uploading file. Please try again !!");
}
return Response.ok("Data uploaded successfully !!").build();
}
}
Test.html
<html>
<body>
<h1>File Upload Example - howtodoinjava.com</h1>
<form action="http://localhost:9080/GetStartedJava/api/upload" method="post" enctype="multipart/form-data">
<p>Select a file : <input type="file" name="file" size="45" accept=".jpg" /></p>
<input type="submit" value="Upload PDF" />
</form>
</body>
</html>