

Single and Multiple File Uploads with Jersey and Bootique
source link: https://nixmash.com/java/single-and-multiple-file-uploads-with-jersey-and-bootique/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Single and Multiple File Uploads with Jersey and Bootique
You'll find information on uploading single and multiple files with Jersey in many places, but there are a few details I'll emphasize in this post which may prove helpful to you. The full source for this post can be found in my File Upload and Download with Jersey Demo App on GitHub. Screenshots of the demo app functions are found in this post.
Here's the Multiple File Upload page from the Demo App. We're using the Bootstrap FileStyle enhancement for the sweet-looking File Input controls and simple JavaScript to add and remove Input controls.

The Controller method handling the upload is shown below. Notice we're receiving a List<FormDataBodyPart> as a parameter. You will often see a FormDataContentDisposition parameter on upload methods which looks like this.
@FormDataParam("file") FormDataContentDisposition fileDetail
But you can get the file details directly from the BodyPart like the filename as we're doing with bodyPart.getContentDisposition() below.
A couple of details that are interesting. (1) is more of a gotcha than a tip, as you want to be sure to read the value of the BodyPart as a FILE. If you retrieve it as an InputStream and process the stream you will end up with file corruption on save.

Along those lines, at (2) we are COPYING the temporary file we just retrieved from the BodyPart. This is because Jersey first saves the file to /tmp as you see in the first entry of the directory listing below. The file naming scheme is MIMExxxxxxxxxxxx.tmp.

You could actually retrieve that file directly through Reflection which would look like this.

So using Apache FileUtils.copy() is a bit more elegant…
Supporting File Upload in Jersey
Before finishing up it's important to remember that you have to add Multipart support to Jersey as laid out in the Jersey Documentation.
First add the dependency.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.27</version>
</dependency>
Then register the MultiPart feature with Jersey. The Jersey Documentation has a couple of examples on that. If you're using Bootique this is what registering the feature would look like in your Module Config.

Source Code Notes for this Post
Source code discussed in this post is found in my File Upload Download in Bootique Jersey Demo app on GitHub.
CategoriesJavaTagsBootique, Java, Javascript, Jersey
Recommend
-
53
bootique - Bootique is a minimally opinionated platform for modern runnable Java apps.
-
57
In this article we will build a fully-functional file upload...
-
12
A Bootique Command Demo Application Using Bootique Commands is a great way to put together tight little JARs to execute scheduled tasks with cron. We'll cover the basics of building a Bootique Command Application in this post.
-
17
Bearer Token Design in Bootique Shiro for REST Authentication My interest in using Bearer Authentication Tokens in Microservices began after reading
-
20
Blueimp Jquery File Upload Plugin with Jersey In my File Upload Download with Jersey Demo App on GitHub you'll find a demo of the Blueimp jQuery File U...
-
13
File Upload Download with Jersey Demo App You'll find a new NixMash demo app on GitHub: NixMash Upload Download with Bootique Jersey. It demos the foll...
-
14
A Bootique Logback Configuration File Example Probably every Bootique Application uses the Bootique Logback Module. All of my Bootique Apps do, yet I've always used a logback.xml file instead of a bootique.yml configuration. One of t...
-
9
Generic Type Injection Examples in Bootique and Guice Back when I was working in Spring I built a Jsoup Annotation-based parsing library which I blogged about here. It...
-
18
NixMash.com is built with the Bootique Java Framework. Modules include Bootique Shiro, Jersey, RabbitMQ Client, MVC, Logback and Jetty. Bootique 0.25 was just released so I didn't waste any time upgrading....
-
9
In this post I'm going to demonstrate using RabbitMQ RPC with the Bootique RabbitMQ Module. You can find RabbitMQ RPC examples elsewhere like in the RabbitMQ Java Examp...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK