Implement file upload using FileUpload components

xiaoxiao2021-03-06  85

Using FileUPload Components Implementing file upload file Upload Upload in web applications, you have to implement file uploads in the servlet / jsp environment. Because there are many components that have been developed on the Internet for file uploading, this article takes a commons-fileupload component Take an example, add a file upload function for the Servlet / JSP application.

The Common-FileUpload component is one of the open source projects of Apache, which can be downloaded from http://jakarta.apache.org/commons/fileupload/. This component is simple and easy to use, upload one or more files at a time, and can limit the file size.

After downloading the zip package, copy the commons-fileupload-1.0.jar to Tomcat's WebApps / Your WebApp / Web-INF / LIB / Under, if the directory does not exist, please ask your own directory.

New Servlet: Upload.java is used for file upload:

Import java.io. *; import java.util. *; import javax.servlet. *; import javax.servlet.http. *; import org.apache.commons.fileUpload. *;

Public class upload extends httpservlet {

Private string UploadPath = "c: // UPLOAD / /"; // TriVate String Temppath = "C: // Upload //TMP //" for storing the upload file; // "// Directory for storing temporary files

Public void dopost (httpservletRequest request, httpservletResponse response) throws oException, servletexception {}}

After the servlet receives the POST request sent by the browser, the file upload is implemented in the dopost () method. The following is a sample code:

public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {try {DiskFileUpload fu = new DiskFileUpload (); // set the maximum file size, this is 4MB fu.setSizeMax (4194304); // set the buffer size, where It is 4KB Fu.setSizetHold (4096); // Setting the temporary directory: fu.setRepositoryPath (Temppath);

// Get all files: list fileItems = fu.parsequest; item i = fileItems.Iterator (); // handles each file: while (I.hasNext ()) {fileItem FI = (FileItem) i .next (); // obtains the file name, this file name includes path: string filename = fi.getname (); if (filename! = null) {// You can record users and file information // ... / / Write file a.txt, you can also extract file name from FileName: FI.WRITE (new file (new file (new file (new file (new file (new file (new file (new file (new file (new file (new file (new file (UPLOADPATH A.TXT ")));}} // Jump to upload success prompt page} catch (Exception e) {// You can jump out the wrong page}} If you want to read the specified upload folder in the configuration file, you can execute it in the init () method:

Public void init () throws servletexception {UPLOADPATH = .... Temppath = .... // Folder does not exist automatically: if (! new file (uploadpa) .Indirectory ()) New file (UploadPath) .mkdirs (); If (! New file (temppath) .Indirectory ()) New file (temppath) .mkdirs ();

Compile this servlet, pay attention to specify ClassPath to ensure that contains commons-upload-1.0.jar and Tomcat / Common / lib / servlet-api.jar.

Configure the servlet, open Tomcat / WebApps / Your webapp / web, INF / Web.xml with Notepad, and create a new one. Typical configuration is as follows:

Upload UPLOAD

Upload / fileupload

After configuring servlet, launch Tomcat, write a simple HTML test:

Note Action = "fileupload" where fileupload is the URL-Pattern that is specified when configuring servlet.

转载请注明原文地址:https://www.9cbs.com/read-107408.html

New Post(0)