How to upload files using REST service with Java, Jersey, JAX-RS on Tomcat
Yes, this might seem obvious but i had to spend a lot of time. Wanted to share to save time for developers like me
TL;DR
Create s REST service in Java to upload a file using JAX-RS, Jersey on Tomcat server.
Clone or download code from Github repo. Instructions on how to build, deploy are included in README file.
Part 1 — Wiring
- Create a new web application using Maven Webapp archetype
- Create a file
ApplicationConfig
under your root java source folder. and registerMultiPartFeature
class.
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.media.multipart.MultiPartFeature;@ApplicationPath("service") // set the path to REST web services
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
packages("com.tf.core").register(MultiPartFeature.class);
}
}
- Add the below dependency in
pom.xml
file.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.27</version>
</dependency>
Apart from above you will also have to add others like
jersey-container-servlet
jersey-server
jersey-container-servlet-core
jersey-hk2
jersey-media-json-jackson
jersey-media-moxy
Part 2 — Creating the service
Create a new class like FileUploadServlet
as below:
@POST
@Path("/image")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public ResponseBean uploadImage(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetails) {
System.out.println(fileDetails.getFileName());
String uploadedFileLocation = "/Users/temp/" + fileDetails.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
ResponseBean responseBean = new ResponseBean();
responseBean.setCode(StatusConstants.SUCCESS_CODE);
responseBean.setMessage(fileDetails.getFileName());
responseBean.setResult(null);
return responseBean;
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Part 3— Build and Test
- Run the command
mvn clean package
to generate awar
file and deploy that or Tomcat. Alternatively you can deploy using your IDE to Tomcat - Test the service on Postman. This post explains this in detail.
Now that you have the service, you may build a front-end that will send the file uploaded to the service.
Full example on Github repo
While all the above may look very obvious and some of you’ll may think why article on such a simple thing? Even with lots of Java development experience, it took me a while to overcome the issues. I’d either see http server exceptions like 500 or 404 or 415. And after a lot of asking around and trial-error, i got the above working.
Hope you enjoyed reading this. I’m always looking for curious people to discuss/talk/write/consult about technology. Ping me up on twitter any time @surenkonathala