The STRUTS2 framework uses the Common-fileupload component by default, which resolves the file domain information in the HttpServletRequest request and uses the IO data stream to save the file at the specified location on the server, completing the file
Upload.
1, Commons-io is a very useful IO toolkit, mainly includes the following 3 aspects:
Utility classes: Provides static methods to accomplish common tasks.
Filters: Provides rich functionality for file filters.
Streams: Execute reader and writer useful stream.
2, the File upload JSP page
In JSP, the Enctype property of the form label should be set to Multipart/form-data.
The Enctype property of the form form is used to specify how the form data is encoded, with the following 3:
application/x-www-form-urlencoded: If you specify this value, the data in the form is encoded as a Key-value pair, which is the default encoding.
Multipart/form-data: That is, using the mine encoding, the form data is processed in binary streams, and the encoding is required for file uploads.
Text/plain: Form data is encoded in plain text without any controls or formatting characters.
3, the File upload action
public class Fileuploadaction extends Actionsupport {
Private file doc;//encapsulates the properties of the uploaded file
Private String filename;//encapsulates the name attribute of an uploaded file
Private String contenttype;//encapsulates the type attributes of an uploaded file
Private String dir;//Save file Path properties
Private String targetfilename;//Save file name attribute
public void Setdoc (file file) {
This.doc = file;
}
public void Setdocfilename (String fileName) {
This.filename = FileName;
}
public void Setdoccontenttype (String contentType) {
This.contenttype = ContentType;
}
...... Omitting partial getter and setter methods
Public String Execute () throws exception{
String Realpath = Servletactioncontext.getrequest (). Getrealpath ("/upload");
String Targetdirectory=realpath;
Targetfilename=generatefilename (fileName);//Build the name of the saved file
Setdir (targetdirectory+ "//" +targetfilename);//save path to File
File target=new filename (targetdirectory,targetfilename);//Create a destination
Fileutils.copyfile (doc, target);//Copy temporary files to destination file
return SUCCESS;
}
Automatically assign file names to uploaded files to avoid duplication
private string Generatefilename (string fileName) {
DateFormat format=new SimpleDateFormat ("Yymmddhhmmss");
String Formatdate=format.format (New Date ());
int random=new random (). Nextint (10000);//The file number is generated
int Position=filename.lastindexof ("."); /get the filename "." The location
System.out.println (FileName);
System.out.println (position);
String extension=filename.substring (position);//Get file extension
SYSTEM.OUT.PRINTLN (extension);
return formatdate+random+extension;
}
}
The action defines a file-type doc attribute that corresponds to the name value of the field in the form in the JSP. At the same time, two very important attributes are defined: FileName and contenttype, which encapsulate information about file uploads:
The Doc property of the file type encapsulates the file contents for that domain.
The string FileName property encapsulates the file name for the file field.
The string-type ContentType property encapsulates the file type for that file field.
Note: In fact, the action uses a setter to encapsulate the 3 parameters of the file field. If the file field name value
is xxx, the action uses setxxx () to encapsulate the file type's contents; use Setxxxfilename () to
Encapsulates the file name, using Setxxxcontenttype () to encapsulate the file type. As long as the action defines the 3 above
method, you can obtain relevant information in the Execute () method.
It is best to define the following constants in Struts.properties:
struts.multipart.savedir=/tmp
The uploaded file is temporarily saved to the TMP folder in the server root directory, and if the folder does not exist, STRUTS2 will automatically create one.
4, Upload file filter
In order to prevent users to upload trojans and other viruses, need to limit the file type of uploading files; In order to prevent server space, you need to limit the size of uploaded files.
A file upload interceptor is built into the STRUTS2 framework. The file upload Interceptor name is FileUpload, as long as the reference to the interceptor when the action is configured to implement the appropriate file filtering. Here's an example:
<action name= "Upload" class= "fileupload.fileuploadaction" >
<interceptor-ref name= "FileUpload" >
The <!-allowedtypes parameter specifies the file type that is allowed to be uploaded-->
<param name= "Allowedtypes" >
Text/plain, Iamge/bmp,image/ppng,image/pjpeg,iamge/gif,image/pjpeg,image/tiff
</param>
The <!-maximumsize parameter specifies the maximum capacity (in bytes) of the uploaded file-->
<param name= "MaximumSize" >10000000</param>
</interceptor-ref>
<interceptor-ref name= "Defaultstack" ></interceptor-ref>
<result name= "Success" >/fileUpLoad/uploadSuccess.jsp</result>
<result name= "Input" >/fileUpLoad/fileUpload.jsp</result>
</action>