The implementation of JSP Servlet File Upload is first to prepare the environment and software:
Common-fileupload component
: Http://jakarta.apache.org/commons/fileupload/
Download and unzip the zip package, copy the commons-fileupload-1.0.jar to tomcat's webapps \ your webapp \ WEB-INF \ lib \
Create a servlet
- Import java. io .*;
- Import java. util .*;
- Import javax. servlet .*;
- Import javax. servlet. http .*;
- Import org. apache. commons. fileupload .*;
-
- Public class Upload extends HttpServlet {
-
- Private StringUploadPath="C: \ upload \\"; // Directory used to store uploaded files
- Private StringTempPath="C: \ upload \ tmp \\"; // Directory used to store temporary files
-
- Public void doPost (HttpServletRequest request, HttpServletResponse response)
- Throws IOException, ServletException
- {
- Try {
- DiskFileUploadFu=NewDiskFileUpload ();
- // Set the maximum file size, which is 4 MB
- Fu. setSizeMax (4194304 );
- // Set the buffer size, which is 4 kb
- Fu. setSizeThreshold (4096 );
- // Set the temporary directory:
- Fu. setRepositoryPath (tempPath );
-
- // Obtain all objects:
- ListFileItems=Fu. ParseRequest (request );
- IteratorI=FileItems. Iterator ();
- // Process each file in sequence:
- While (I. hasNext ()){
- FileItemFi= (FileItem) I. next ();
- // Get the file name, which includes the path:
- StringFiFileName= Fi. getName ();
- If (fileName! = Null ){
- // User and file information can be recorded here
- //...
- // Write the.txt file. You can also extract the file name from fileName:
- Fi. write (new File (uploadPath + "a.txt "));
- }
- }
- // Jump to the upload success prompt page
- }
- Catch (Exception e ){
- // Jump to the error page
- }
- }
- }
After the Servlet receives a Post request from the browser, it uploads the file in the doPost () method. The following is the sample code:
To read the specified upload folder in the configuration file, run the following command in the init () method:
- Public void init () throws ServletException {
- UploadPath= ....
- TempPath= ....
- // The folder is automatically created if it does not exist:
- If (! New File (uploadPath). isDirectory ())
- New File (uploadPath). mkdirs ();
- If (! New File (tempPath). isDirectory ())
- New File (tempPath). mkdirs ();
- }
Servlet File Upload configuration Servlet, open tomcat \ webapps \ your webapp \ WEB-INF \ web. xml with notepad, if not, create a new one. The typical configuration is as follows:
- ﹤?xml version="1.0" encoding="ISO-8859-1"?﹥
- ﹤!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd"﹥
- ﹤web-app﹥
- ﹤servlet﹥
- ﹤servlet-name﹥Upload﹤/servlet-name﹥
- ﹤servlet-class﹥Upload﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥Upload﹤/servlet-name﹥
- ﹤url-pattern﹥/fileupload﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
- ﹤/web-app﹥
End of Servlet File Upload: After configuring the Servlet, start Tomcat and write a simple html test:
- ﹤form action="fileupload" method="post" enctype="multipart/form-data" name="form1"﹥
- ﹤input type="file" name="file"﹥
- ﹤input type="submit" name="Submit" value="upload"﹥
- ﹤/form﹥
Note action = "fileupload" where fileupload is the url-pattern specified When configuring the servlet.
So the JSP Servlet File Upload implementation is complete. Do you have any changes in development ideas?
- What is JSP and comparison with Servlet?
- 8-point discussion on optimizing JSP Servlet applications
- Servlet import event-driven technology in JSP development
- Configuration of JSP, Servlet, and Bean in Tomcat
- How to Improve Servlet and JSP Application Efficiency