Java Reference
In-Depth Information
Enhanced FileUpload
The Servlet 3.0 API included a new FileUpload mechanism that allows a servlet to be specified as a file upload
handler, making it unnecessary for third-party libraries to enable file uploading. As a brief review, any servlet that is
marked with the @MultipartConfig annotation can handle file upload requests. The processing for saving the file to
disk then occurs within the file upload servlet. The @MulipartConfig annotation can accept the following attributes,
which can be used to specify details for the file upload:
location : Absolute path to the directory on the file system
fileSizeThreshold : File size in bytes after which the file will be temporarily stored on disk
maxFileSize : Maximum file size accepted, in bytes
maxRequestSize : Maximum size allowed for the multipart/form-data request, in bytes
For example, suppose that an HTML page contained a file input and the request was sent to a servlet named
FileUploadHandler . The code for the FileUploadHandler servlet may look like the following:
@MultipartConfig
@WebServlet(name = "FileUploadHandler", urlPatterns = {"/uploadFile"})
public class FileUploadHandler extends HttpServlet {
private final static Logger LOGGER =
Logger.getLogger(FileUploadHandler.class.getCanonicalName());
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
 
Search WWH ::




Custom Search