2016-08-31
First, File upload
- Using Commons-fileupload-1.2.1.jar to achieve a simple upload file, first fill out the form on the page, remember to add enctype= "Multipart/form-data", mainly to ensure the correct encoding of the uploaded file, Set the MIME encoding of the form so that the input value in the form is passed in 2, so it can be used to upload the file.
2. Configure Struts.xml, complete the FileUpload and Action instance mapping, here upload a picture file as an example
< Action name="fileUpload " class= "action. Uploadaction "method="upload">
<interceptor-ref name="FileUpload"> //Set FileUpload filter Manager
<param name="Allowedtypes">image/pjpeg,image/jpg,im Age/jpeg,image/bmp,
Image/gif,image/png </ param > //Set the types of files allowed to upload
<param name="MaximumSize">2097152</param> //upload Maximum size of file
</interceptor-ref>
<interceptor-ref name="Defaultstack"></interceptor-ref> /c4>//Inherit the default interceptor
<param name="Savepath">/upload/file</param> //settings File save path for upload
<result>/upload/success.jsp </ Result> //Jump page after success
<result name="input">/upload/upload.jsp</result> //Internal Department failed to return input
</action>
After Setup, create the path/upload/file under Webroot, and then configure the Tomcat build folder to hold the upload file.
3. Then write the corresponding uploadaction
Public class Uploadaction extends actionsupport {
Private File pic; The pic here can be named XXX by itself
Private String Piccontenttype; Here the naming format must be xxxcontenttype with the previous file name
Private String Picfilename; Xxxfilename maybe the same.
Private String nickname;
Private String Savepath; Get parameter Savepath
Public String upload () {
System. out. println ("ContentType:" +piccontenttype);
System. out. println ("FileName:" +picfilename);
System. out. println ("nickname:" +nickname);
System. out. println ("Savepath:" +savepath);
Bufferedinputstream bis = null;
Bufferedoutputstream BOS = null;
Try {
bis = new bufferedinputstream (new FileInputStream (pic)); Gets the input stream of the file
ServletContext application = Servletactioncontext. Getservletcontext ();
Getrealpath get file output relative path the actual path on the hard disk is flat behind and the file name is passed in to upload it successfully
BOS = New bufferedoutputstream (new FileOutputStream (Application.getrealpath (
Savepath) + "/" +picfilename));
int r = 0;
while ((R=bis.read ())!=-1) {
Bos.write (R);
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Try {
if (bis!=null) {
Bis.close ();
}
if (bos!=null) {
Bos.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
return SUCCESS;
}
In addition, don't forget to add the Setter/getter method at last.
4. Finally complete the upload after successful display page success.jsp
Now it's time to start the Tomcat server test and upload a picture on the desktop after entering upload.jsp,
Upload success!
Two, multiple file upload
Multiple file uploads are usually caught in an array in action, and then the output is iterated, essentially the same steps.
First write the upload page, here upload 2 pictures.
<action name="filesupload" class= "action. Uploadsaction "method="upload">
<interceptor-ref name="FileUpload">
<param name="Allowedtypes">
Image/pjpeg,image/jpg,image/jpeg,image/bmp,image/gif,image/png</param>
<param name="MaximumSize">4097152</param>
</interceptor-ref>
<interceptor-ref name="Defaultstack"></interceptor-ref>
<param name="Savepath">/upload/file</param>
<result>/upload/success2.jsp</result>
<result name="input">/upload/upload2.jsp</result></action>
The Struts.xml configuration is followed.
This action uploads the text with an array to catch the line.
The last iteration of the file name, showing the picture.
Keep a record of today's learning content, if there is anything to say that is not clear where the welcome supplement
Simple File Upload in struts2