= = = (1) The first, the use of ordinary buffer stream for file upload
① Front End
Attention:
1. Specify the form type for file upload form: enctype= "Multipart/form-data"
2, the way must be submitted: Post
3. form, the form element of the file field exists
<form name= "Frm_test" action= "${pagecontext.request.contextpath}/shangchuan" method= "post" enctype= "multipart/ Form-data ">
<%--<input type= "hidden" name= "method" value= "Upload" >--%>
User name: <input type= "text" name= "UserName" > <br/>
File: <input type= "file" name= "file_img" > <br/>
<input type= "Submit" value= "Submission" >
</form>
② Backstage
Attention:
Request.getquerystring ();//Get data for Get submissions
Request.getinputstream ();//Get post-submitted data, post requests are sent as streams, (InputStream)
@RequestMapping ("Shangchuan")
Public String Shangchuan (HttpServletRequest request,httpservletresponse respose) throws Servletexception,ioexception {
Get form (POST) data
InputStream In=request.getinputstream ();
Convert stream
InputStreamReader inreader=new InputStreamReader (in);
Buffered streams
BufferedReader reader=new BufferedReader (Inreader);
Traverse
String Str=null;
while ((Str=reader.readline ())!=null) {
System.out.println ("-=------------------------------------------------" +str);
Parse the uploaded data
}
return null;
}
= = = (2,) the second, FileUpload components (upload components)! Apache offers open source projects!
Note:
*******************************************************
1. Downloading components; importing jar files
commons-fileupload-1.2.1
commons-io-1.4
2. Configuration (Prop/xml)
3, FileUpload's API Learning
*①filefactory File Upload factory class (encapsulates each request as a Fileitem object)
* Fileitemfactory.setrepository (repository) Set File Upload temp path (with default)
*
*②servletfileupload File Upload Core class
* List = upload.parserequest (Request)
* Upload.ismultipartcontent (Request) Judgment form type: File Upload form
*//One, set the maximum size allowed for a single file: 30M
Upload.setfilesizemax (30*1024*1024);
Second, set the file upload form the total allowed size: 80M
Upload.setsizemax (80*1024*1024);
Third, set the encoding of the file name of the upload form
Equivalent: Request.setcharacterencoding ("UTF-8");
Upload.setheaderencoding ("UTF-8") is equivalent to Request.setcontenttype (..);
③fileitem encapsulates the value of the normal form item and the file upload form element value
Item.getfilename () Get upload form element name
Item.getstring () Get Upload data
Item.getstring ("UTF-8") get upload data, handle Chinese garbled
Item.getcontentype () Get upload file type "file entry"
Item.getinputstream () Get file stream "file entry"
Item.getname (); Get filename "File entry"
Item.write ("File path"); Write files to the specified directory
Item.delete Deleting temporary files
*/
*******************************************************
* ##################################
!!!!! Requirements for this feature:!!!!!
* Implementation of a full file upload
* 1, set a single file can not exceed 30M
* 2, set total size cannot exceed 50M
* 3, upload the directory, uploaded to the project resource directory under the upload directory
* 4, upload files can not be overwritten, to resolve the file name of upload files do not duplicate
* ##################################
@RequestMapping ("Shangchuan")
Public String FileUpload (HttpServletRequest request,httpservletresponse respose) throws Servletexception,ioexception {
1. create File Upload Workshop class
Fileitemfactory fileitemfactory=new diskfileitemfactory ();
2. Create a file Upload Core class object
Servletfileupload upload=new servletfileupload (fileitemfactory);
Upload.setfileitemfactory (fileitemfactory);
"One, set single file Max: 30M"
Upload.setfilesizemax (30*1024*1024);//30m
"Two, set total file size: 50M"
Upload.setsizemax (50*1024*1024);
3. Determine if the current form is a file upload form
if (upload.ismultipartcontent (request)) {
4. Convert request data to Fileitem object collection * (Fileitem represents each upload primary key)
try {
List<fileitem> list=upload.parserequest (Request);
5, traverse, get each upload item
for (Fileitem item:list) {
6, when the traversal to determine whether it is a normal form or file upload data
if (Item.isformfield ()) {
Normal forms
Get element name
String Filename=item.getfieldname ();
Get value
String value=item.getstring ("UTF-8");
System.out.println (Filename+value);
}else{
File Upload form (corresponding to front end type=file)
/******** File Upload ***********/
Handling file Uploads
Get file name
String Name=item.getname ();
Regenerate file name
Get the uploaded directory path
String basepath=request.getsession (). Getservletcontext (). Getrealpath ("/upload");
To create a file object
File file =new file (basepath,name);
Write file
Item.write (file);
Item.delete ();//Delete temporary files
}
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}else{
System.out.println ("The current form is not a file upload form, do not process");
}
return null;
}
Learn a small feature every day: Java File upload