Now whether it is a blog forum or corporate office, can not be separated from the sharing of resources. Through the way of file upload, and share with you, so as to achieve a wide range of communication and communication between the masses, we can gain more knowledge and experience, but also through the feedback of others to achieve self-improvement and promotion purposes.
Next I will introduce the Web project in this upload function, then how the file is sent from the local to the server. Watch me slow down:
First, we create a new Web project that creates a upload folder in the Webroot directory of the project, so that when we deploy the project to the server, the server also generates a upload folder to store the uploaded resources.
Then, in the Webroot directory to create a new JSP file, the main implementation of the role is to select the uploaded files, submitted to the servlet to deal with
The detailed code is as follows: A form transmits file information via post to the specified servlet
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > As you can see, we submit the data to the Upload/upload under the project.
And then we're going to write this Servlet--upload.java
Package load;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.List;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.commons.fileupload.FileItem;
Import org.apache.commons.fileupload.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload; public class UpLoad extends HttpServlet {@SuppressWarnings ("unchecked") @Override protected void Service (Httpservletre
Quest req, HttpServletResponse resp) throws Servletexception, IOException {req.setcharacterencoding ("utf-8");
Resp.setcontenttype ("Text/html;charset=utf-8");
Provides configuration information for the parsing class diskfileitemfactory factory = new Diskfileitemfactory ();
Create an instance of the parse class servletfileupload SFU = new Servletfileupload (factory);
Start parsing Sfu.setfilesizemax (1024*400);
The data in each form field is encapsulated into a corresponding Fileitem object on the try {list<fileitem> items = sfu.parserequest (req);
Differentiate form fields for (int i = 0; i < items.size (); i++) {Fileitem item = items.get (i);
Isformfield is true to indicate that this is not a File Upload form field if (!item.isformfield ()) {ServletContext sctx = Getservletcontext ();
Get the physical path that holds the file//upload a folder under the current online users find the corresponding folder String path = Sctx.getrealpath ("/upload");
SYSTEM.OUT.PRINTLN (path);
Get filename String filename = item.getname ();
System.out.println (FileName);
In some platforms (operating systems), the method returns the path + filename filename = filename.substring (Filename.lastindexof ("/") +1);
File File = new file (path+ "\" +filename);
if (!file.exists ()) {item.write (file);
The name of the uploaded image is recorded in the database Resp.sendredirect ("/upload/ok.html");
catch (Exception e) {e.printstacktrace ()}}};
}
}
}
Because the code has been a detailed annotation, so I believe you can basically pass this process. One thing to note is the setting that resolves the size of the instance space. We want the uploaded file to not be infinitely large, so set the
. Setfilesizemax (1024*400);
Here we can also set it as a condition to submit an error message to the page when the file is larger than the maximum value. In addition, you can also read the suffix of the selection file to filter out the types that can be uploaded. These codes are extended by yourselves and are no longer in detail.
Through the servlet, the correct files are routed to the upload folder on the server side. Notice here that if you remove the project from Tomcat later, the files will be deleted automatically.
After the upload, the page ok.html to the Upload Success page. When the user sees this page, it indicates that you have implemented the file upload function.