1. The need to import jar package
2.index.html
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > the Insert title here Wallpaper </ title > </ head > < body > <-! To utilize DiskFileItemFactory Upload Files in jsp or html page, form a form method must be set to post, and to set = the enctype "multipart / form-Data": -> < form Action = "the FileServlet" method = "POST" the enctype = "multipart / form- the Data " > Name: < the INPUT of the type="text" name="uname"> 密码:<input type="password" name="pwd"> 文件:<input type="file" name="myfile"> <input type="submit" value="提交"> </form> </body> </html>
3.FileServlet.java servlet receives the file code
package web; import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; 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; importorg.apache.commons.fileupload.servlet.ServletFileUpload; / * * @WebServlet marked for annotation on a inherited HttpServlet class, belonging to the class level annotation * where / FileServlet url indicates access the servlet mapping (address) ( here a relative path, i.e., "item name / the FileServlet") * the role equivalent to the annotation servlet configuration in web.xml <servlet-mapping> element <url-pattern> configuration * * / @ WebServlet ( "/ the FileServlet" ) public class the FileServlet the extends HttpServlet { // extend HttpServlet class // serialVersionUID role is to maintain compatibility version, i.e. deserialize objects remain in the version upgrade serialization uniqueness, the version control Private static Final Long serialVersionUID = 1L ; // processing method GET method request protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { // create a parser facility DiskFileItemFactory Factory = new new DiskFileItemFactory (); // file upload parser ServletFileUpload = Upload new new ServletFileUpload (Factory); // determines whether the attribute enctype is a multipart / form-Data Boolean isMultipart = ServletFileUpload.isMultipartContent (request); IF (isMultipart) { the try { // parse the request, the entry form for each object is encapsulated into a FileItem List <the FileItem> fileItems = upload.parseRequest (Request); // iteration form data for (the FileItem FileItem: fileItems) { // determine the type of input ordinary file entry or IF (fileItem.isFormField ()) { // Normal entry, obtain the value of the name attribute of the input, fileItem.getFieldName () //// obtained values of the input items, fileItem.getString ( "UTF-8" ), "UTF-8" Chinese prevent distortion System.out .println (fileItem.getFieldName () + "\ T" + fileItem.getString ( "UTF-. 8" )); } the else { // upload a file, get file upload the file name field //Note IE or FireFox acquired file name is not the same, IE is an absolute path, FireFox only the file name String fileName = fileItem.getName (); System.out.println (fileName); // the Substring string is taken , the return value is a string taken after // string after lastIndexOf ( ".") from right to left to check, obtaining string EXT = fileName.substring (fileName.lastIndexOf ( "." )); // . UUID.randomUUID () toString () is a method of automatically generating a primary key javaJDK provided only drawback is that the results generated UUID string will be longer String name = UUID.randomUUID () + EXT; // save the object FileItem the main content saved to a file specified by file file = new new File("E:\\火影忍者\\"+name); fileItem.write(file); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("普通表单"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }