Current file Upload (framework) components: Apache----fileupload, orialiy–cos–2008 (), jsp-smart-upload–200m. Uploading Files with FileUpload:
You need to import a third-party package:
apache-fileupload.jar– File Upload core package.
apache-commons-io.jar– This package is a fileupload dependency package. It's also a toolkit.
Use SPRINGMVC to solve annoying post garbled issues and create a new MAVEN project:
Maven dependencies:
<Dependency> <groupId>Commons-fileupload</groupId> <Artifactid>Commons-fileupload</Artifactid> <version>${commons-fileupload.version}</version></Dependency><Dependency> <groupId>Commons-io</groupId> <Artifactid>Commons-io</Artifactid> <version>${commons-io.version}</version></Dependency>
Version:
<commons-io.version>1.3.2</commons-io.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
Core class:
diskfileitemfactory– Set disk space to save temporary files. Just a class.
Servletfileupload-The core class of file uploads, this class receives the request, and resolves the reqeust
Servletfileupload.parserequest (Request); --list<fileitem> Parsing Request
A fileitem is an identity delimiter beginning to the end. Such as:
View Diskfileitemfactory source Code,
Defaultfollows: Size threshold is 10KB. default Temp directory, as returned by System.getproperty ("Java.io.tmpdir")
If you do not set the temporary directory, it will be saved in the default temporary directory-system.getproperty ("Java.io.tmpdir"), this directory is the Windows system temporary file storage directory, through the environment variable, you can find this directory
There are a lot of temporary files stored here.
Controller
PackageCom.lhy.upload.controller;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;Importjava.util.List;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.commons.fileupload.FileItem;Importorg.apache.commons.fileupload.FileUploadException;Importorg.apache.commons.fileupload.disk.DiskFileItemFactory;ImportOrg.apache.commons.fileupload.servlet.ServletFileUpload;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;/*** * Diskfileitemfactory: Set disk space, save temporary files, just a tool class. * Constructor diskfileitemfactory (int sizethreshold, File repository): * Sizethreshold: * Set Cache Save (memory) to save how many bytes of data, default 10K. * If a file is not larger than 10K, use the memory directly and save it directly to the file. * If a file is larger than 10K, you will need to save the file to a temporary file first. * Repository: * location of temporary directory. * * * * servletfileupload: File Upload core class, receive request and parse. * */@RequestMapping ("/upload") @Controller Public classUploadcontroller {@RequestMapping ("UploadFile") Public voidUploadFile (httpservletrequest request,httpservletresponse response) {//get the path to the up directory under TomcatString path = Request.getsession (). Getservletcontext (). Getrealpath ("/up"); //1, declaring the Diskfileitemfactory factory class, for setting up a temporary directory on the specified diskDiskfileitemfactory disk =NewDiskfileitemfactory (1024*10,NewFile ("F:/temp")); //2, declare servletfileupload, receive the above temporary file. can also be the default valueServletfileupload up =Newservletfileupload (disk); //3, parse request Try{List<FileItem> list =up.parserequest (Request); //If you have a file,Fileitem file = List.get (0); //Get file name:String FileName =File.getname (); //get the type of file:String FileType =File.getcontenttype (); //get the byte code of the file:InputStream in =File.getinputstream (); //File Size intSize =File.getinputstream (). available (); //declaring the output byte streamOutputStream out =NewFileOutputStream (path+ "/" +fileName); //file Copy byte[] B =New byte[1024]; intLen = 0; while((Len=in.read (b))!=-1) {Out.write (b,0, Len); } out.flush (); Out.close (); //Delete upload-generated temporary filesFile.delete (); //Show DataResponse.setcontenttype ("Text/html;charset=utf-8"); PrintWriter PW=Response.getwriter (); Pw.println ("File name:" +fileName); Pw.println ("File type:" +FileType); Pw.println ("<br/> file Size (Byte):" +size); } Catch(fileuploadexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Upload file:
Temp directory:
Service side:
Response:
The actual project is a file server, the company generally provides the upload to the file server interface, there are some upload one file type, there are some streams.
Multiple file uploads: As with single files
Form:
Controller
/*** Multi-File Upload *@paramRequest *@paramResponse*/@RequestMapping ("Uploadmultipart") Public voidUploadmultipart (httpservletrequest request,httpservletresponse response) {//get the path to the up directory under TomcatString path = Request.getsession (). Getservletcontext (). Getrealpath ("/up"); //1, declaring the Diskfileitemfactory factory class, for setting up a temporary directory on the specified diskDiskfileitemfactory disk =NewDiskfileitemfactory (1024*10,NewFile ("F:/temp")); //2, declare servletfileupload, receive the above temporary file. can also be the default valueServletfileupload up =Newservletfileupload (disk); //3, parse request Try{List<FileItem> list =up.parserequest (Request); for(Fileitem file:list) {//Get file name:String FileName =File.getname (); //get the type of file:String FileType =File.getcontenttype (); //get the byte code of the file:InputStream in =File.getinputstream (); //using the tool classFileutils.writebytearraytofile (NewFile (path+ "/" +fileName), File.get ()); File.delete (); } } Catch(fileuploadexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
Test, upload 3 photos:
Java file upload-using the Apache-fileupload component