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.
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.
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.
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.
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">Handling the POST in processRequest method of Servlet
<input type="file" name="ufile" />
<input type="submit" value="Submit" />
</form>
protected void processRequest(HttpServletRequest request,Getting the uploaded file Part
HttpServletResponse response)
throws ServletException, IOException {
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 formatWe will call the helper function like this
//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;
}
final String fileName = getFileName(filePart);
//we will construct the path to save the file.Reading from uploaded file Stream and writing to local file
//Right now I am making dummy path for simplicity
String filePath="D:\\"+fileName;
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();Start reading from InputStream and write to OutputStream
OutputStream fileOut = new FileOutputStream(new File(filePath));
int read = 0;Closing the streams
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes,0,1024)) != -1) {
out.write(bytes, 0, read);
}
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
Comments
Post a Comment
Share your wisdom