JEE File Upload - Handling Multipart requests in Servlets 3.0
Prior toĀ Servlets 3.0Ā developers have to use 3rd party libraries to process complex multipart request that is sent when file is uploaded by user. Starting from Servlets 3.0 JEE provides a better way to handle file uploads (multipart requests) without using any 3rd party library. Making your Servlet ready to handle Multipart requests All you have to do is to addĀ @MultipartConfigĀ annotation on your servlet class and it is ready to handle Multipart requests.
@MultipartConfigĀ
public class FileUploadServlet extends HttpServlet {
Our example HTML form
<form method="POST" enctype="multipart/form-data"> <input type="file" name="ufile" /> <input type="submit" value="Submit" /> </form>
Handling the POST in processRequest method of Servlet
protected voidĀ processRequest(HttpServletRequest request, Ā Ā Ā Ā HttpServletResponse response) Ā Ā Ā Ā throws ServletException, IOException {
Getting the uploaded file Part To extract the uploaded file we will useĀ getPartĀ method of HttpServletRequest. getPart method returns a part from multipart request specified by name passed as parameter to it.
Part filePart = request.getPart("ufile");
Getting file name of uploaded file Even though we have now support in servlets to handle Multipart request we have to do some extra processing to get the file name and other things from the Part. We will make a helper function that will return the filename passed from client's browser to our server in the <input type=file> object.
//Content-Disposition for uploaded file has following format //form-data; name="ufile";Ā filename="name.txt" private StringĀ getFileName(final Part part) { Ā Ā final String partHeader = part.getHeader("content-disposition"); Ā Ā for (String content : part.getHeader("content-disposition").split(";")) { Ā Ā Ā Ā if (content.trim().startsWith("filename")) { Ā Ā Ā Ā Ā Ā return content.substring( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā content.indexOf('=') + 1).trim().replace("\"", ""); Ā Ā Ā Ā } Ā Ā } Ā Ā return null; }
We will call the helper function like this
final StringĀ fileNameĀ = getFileName(filePart);
//we will construct the path to save the file. //Right now I am making dummy path for simplicity StringĀ filePath="D:\\"+fileName;
Reading from uploaded file Stream and writing to local file As standard in Java we will needĀ OutputStreamĀ of fileĀ where we will store our uploaded fileĀ andĀ InputStreamĀ forĀ uploaded file. Part class has a methodĀ getInputStream()Ā that returns the InputStream to read the bytes/data of that Part.
InputStream filecontent = filePart.getInputStream(); OutputStream fileOut = new FileOutputStream(new File(filePath));
Start reading from InputStream and write to OutputStream
int read = 0; final byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes,0,1024)) != -1) { Ā Ā out.write(bytes, 0, read); }
Closing the streams
if (out != null) { Ā Ā out.close(); } if (filecontent != null) { Ā Ā filecontent.close(); }















