Here we introduce the knowledge of file uploading, using the Apache Commons FileUpload framework.
Use of file uploads
The parts of the project are structured as follows:
First, using the upload function of Commons fileupload, we need to introduce two jar packages: Commons-fileupload and Commons-io. First we list the parts of the HTML
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Huhx1.html</title></Head><Body> <formMethod= "POST"enctype= "Multipart/form-data"Action= "Fileuploadservlet">File to Upload1:<inputtype= "File"name= "Upfile1"><BR/>File to Upload2:<inputtype= "File"name= "Upfile2"><BR/>Notes about the file:<inputtype= "text"name= "Note"><BR/> <BR/> <inputtype= "Submit"value= "Press">To upload the file! </form></Body></HTML>
Second, Fileuploadservlet is a servlet, the content is as follows:
protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {diskfileitemfactory factory=Newdiskfileitemfactory (); ServletContext ServletContext= This. Getservletconfig (). Getservletcontext (); File Repository= (File) servletcontext.getattribute ("Javax.servlet.context.tempdir"); Factory.setrepository (repository); Servletfileupload Upload=Newservletfileupload (Factory); Try{List<FileItem> items =upload.parserequest (Request); Iterator<FileItem> iter =Items.iterator (); while(Iter.hasnext ()) {Fileitem Item=Iter.next (); if(Item.isformfield ()) {String name=Item.getfieldname (); String value=item.getstring (); System.out.println ("Name:" + name + ", Value:" +value); } Else{String FieldName=Item.getfieldname (); String FileName=Item.getname (); String ContentType=Item.getcontenttype (); BooleanIsInMemory =item.isinmemory (); LongsizeInBytes =item.getsize (); File UploadedFile=NewFile (Request.getservletcontext (). Getrealpath ("File"), fileName); System.out.println (Uploadedfile.getabsolutepath ()); Item.write (UploadedFile); } } } Catch(Exception e) {e.printstacktrace (); }}
You need to create a file folder under the directory of the deployment project, as in. Request.getservletcontext (). Getrealpath ("file") code
Spring MVC uploads files
First, configure the uploaded information in the configuration file
<!--support for uploading files -<BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Propertyname= "Defaultencoding"value= "UTF-8" /> < Propertyname= "Maxuploadsize"value= "5000000" /></Bean>
Commonsmultipartresolver is actually a encapsulation of the above common-fileupload.
Second, in the Java class: Multi-File upload requires @requestparam (value= "Files"), files represents the name of the definition file. For Formdata, that's the key to the file.
Public voidFileuplaod (HttpServletRequest request, @RequestParam (value= "Files") multipartfile[] files, httpservletresponse response) {System.out.println (files.length); Responseresult result=NewResponseresult (); for(Multipartfile file:files) {Try{fileutils.copyinputstreamtofile (File.getinputstream (),NewFile (Realpath, File.getoriginalfilename ())); } Catch(IOException E1) {e1.printstacktrace (); }} Map<string, string> map =NewHashmap<>(); Result.setreturncode (Constants.success_return_code); Result.setdata (map); Responseutils.returnresponsejsondata (response, result);}
Third, in order to prevent duplicate file submission, you need to compare the file information
String newfilecontent = file.getoriginalfilename () +file.getsize (); String Newencryfile=Encryptutils.sha1encrypt (newfilecontent);//in practical use, request.getsession (). getattribute ("FileCheck") is obtained from the databaseif(Newencryfile.equals (Request.getsession (). getattribute ("FileCheck")) {result.setreturncode (Constants.fail_return_code); Result.seterrormsg (constantsmsg.file_upload_repeat); Responseutils.returnresponsejsondata (response, result); return;}// .... After the upload is complete, the encryfile will exist in the databaseString filecontent = file.getoriginalfilename () +file.getsize (); String Encryfile=Encryptutils.sha1encrypt (filecontent); Request.getsession (). SetAttribute ("FileCheck", encryfile);
The principle of FileUpload
Follow-up supplement
Friendship Link
- FileUpload's Official document: http://commons.apache.org/proper/commons-fileupload/using.html
Web base----upload of >fileupload files