|
File Upload with Servlet 3.0
This code works on Glassfish 3. This is a simple example of how to upload files using JSP and Servlet 3.0 which is a part of Java EE 6. While in earlier versions of Servlets we had to use commons fileupload or other libraries, this feature has been integrated into the Servlet 3.0 specification. Here is the code for a simple servlet and a JSP file that makes a file upload request to the servlet. 1.) FileUploadServlet.java package com.blogspot.aoj.servlet3; import java.io.FileOutputStream;
import javax.servlet.ServletException;
import org.apache.log4j.Logger; @WebServlet(urlPatterns = "/fileUpload")
public FileUploadServlet()
{
protected void doGet(HttpServletRequest
request,
} private String getFileName(Part
part) {
} protected void doPost(HttpServletRequest
request,
} Note: There is no need to use a deployment descriptor the @WebServlet annotation is enough, which uses the urlPatterns property to define servlet mappings. The @MultipartConfig is used to indicate that the servlet expects requests wof multipart/form-data MIME type which is required for file upload. Using @MultipartConfig allows you to use the request.getParts get the parts of the request. The getFileName method gets the
file name from the content-disposition header.
2.) upload.jsp <html>
|
|
See also
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|