Trying to upload a file to a JAX-RS (jersey) server
If you want to add Strings to the FormDataMultiPart
just use the .field("name", "value")
method the same way it is used for the file attachment (queryParam does not work).
Below is a working sample:
First, the server part which returns the content of the read file as a String:
@Path("file")
public class FileResource {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response handleUpload(@FormDataParam("file") InputStream stream) throws Exception {
return Response.ok(IOUtils.toString(stream)).build();
}
}
Second, the client method posting the file:
public void upload(String url, String fileName) {
InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName);
FormDataMultiPart part = new FormDataMultiPart().field("file", stream, MediaType.TEXT_PLAIN_TYPE);
WebResource resource = Client.create().resource(url);
String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
assertEquals("Hello, World", response);
}
Third, the test environment:
Server server;
@Before
public void before() throws Exception {
server = new Server(8080);
server.addHandler(new WebAppContext(WEB_INF_DIRECTORY, "/"));
server.start();
}
@After
public void after() throws Exception {
server.stop();
}
@Test
public void upload() {
upload("http://localhost:8080/file", "file.txt");
}
Finally, the maven dependencies:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-embedded</artifactId>
<version>6.1.26</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
The file.txt
is at the root of the classpath and contains Hello, World
.
Yves solution didn't work for me on the client side. I looked around a bit and found:
- http://puspendu.wordpress.com/2012/08/23/restful-webservice-file-upload-with-jersey/
- http://neopatel.blogspot.de/2011/04/jersey-posting-multipart-data.html
- http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
non of which would work with my current jersey 1.18 (see pom extract below). Most trouble was on the client side. I would get error messages like:
com.sun.jersey.api.client.ClientHandlerException: javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Missing body part entity of type 'text/plain'
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
The server side worked quickly with this code (which doesn't do anything interesting with the uploaded InputStream yet - fit to your needs )
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
public Response uploadFile(
@FormDataParam("content") final InputStream uploadedInputStream,
@FormDataParam("fileName") String fileName) throws IOException {
String uploadContent=IOUtils.toString(uploadedInputStream);
return Response.ok(uploadContent).build();
}
the client side would work with this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
/**
* upload the given file
*
* inspired by
* http://neopatel.blogspot.de/2011/04/jersey-posting-multipart-data.html
*
* @param url
* @param uploadFile
* @return the result
* @throws IOException
*/
public String upload(String url, File uploadFile) throws IOException {
WebResource resource = Client.create().resource(url);
FormDataMultiPart form = new FormDataMultiPart();
form.field("fileName", uploadFile.getName());
FormDataBodyPart fdp = new FormDataBodyPart("content",
new FileInputStream(uploadFile),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp);
String response = resource.type(MediaType.MULTIPART_FORM_DATA).post(String.class, form);
return response;
}
pom.xml extract:
<properties>
<jersey.version>1.18</jersey.version>
</properties>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- Multipart support -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>