A summary of the implementation of the file upload function in the project:
1. Environment construction
To set up the Web engineering process omitted, only need to add the following two jar packages in the project:
Commons-fileupload-1.2.1.jar
Commons-io-2.0.jar resources available for download online
2. Pre-paragraph preparation
<formAction= "Uploadservlet"Method= "POST"enctype= "Multipart/form-data">FileName:<inputtype= "text"name= "FileName"Accept= "Image/gif,image/jpeg,image/png">File:<inputtype= "File"name= "File"/> <inputtype= "Submit"value= "Submit"/></form>
Note: I. form is submitted as post
II. form fields using file: <input type= "file" name= "file" accept= "Image/gif,image/jpeg,image/png"/> Accept to restrict upload file types
III. Request encoding using Multipart/form-data; data will be transmitted in binary form
3. Server-Side Preparation
Create Uploadservletu to provide services
The key code is as follows:
Note: Because the form is transmitted in binary form, garbled characters may appear on the server. My pro-test: Using string fieldvalue = Item.getstring ("Utf-8"), can effectively solve
Text field Chinese garbled problem.
//1. Get a collection of FileitemDiskfileitemfactory factory =Newdiskfileitemfactory ();//set the maximum size of the uploaded file in memory and write the file to a temporary folder if it is exceeded. In bytesFactory.setsizethreshold (1024 * 500);//Set the Temp folderFile tempdirectory =NewFile ("/resources/tempdirectory"); factory.setrepository (tempdirectory); Servletfileupload Upload=Newservletfileupload (Factory); Upload.setheaderencoding ("Utf-8");//sets the total size of the uploaded file. You can also set the size of a single file.Upload.setsizemax (1024 * 1024 * 5); InputStream in=NULL; OutputStream out=NULL;//Parse RequestTry{List<FileItem>/*Fileitem*/Items =upload.parserequest (request);//2. Traverse the Fileitem collection if the form field prints information otherwise it is saved on disk for(Fileitem item:items) {if(Item.isformfield ()) {String FieldName=Item.getfieldname (); String Fieldvalue= Item.getstring ("Utf-8"); System.out.println (FieldName+": " +fieldvalue); } Else{String FieldName=Item.getfieldname (); String FileName=Item.getname (); String ContentType=Item.getcontenttype (); LongsizeInBytes =item.getsize (); //determine if the form domain name is img if(Fieldname.equals ("img")) {//determine if a picture type if(Contenttype.equals ("Image/png") | |Contenttype.equals ("Image/jpeg") | |Contenttype.equals ("Image/gif")) {//determine if the picture is more than 5M if(sizeInBytes <= 5 * 1024 * 1024) { in=Item.getinputstream (); byte[] buffer =New byte[1024]; intLen = 0; FileName= System.currenttimemillis () +FileName; IMG=FileName; FileName= This. Getservletcontext (). Getrealpath ("/") + "resources\\imgs\\" +FileName; System.out.println (FileName); out=NewFileOutputStream (fileName); while(len = in.read (buffer))! =-1) {out.write (buffer,0, Len); } }}}}}Catch(fileuploadexception e) {e.printstacktrace ();}finally{if(Out! =NULL) {out.close (); }if(In! =NULL) {in.close (); }}
The sixth time operation of modern software engineering