In the B/s system, there will often be file upload download requirements, now based on the STRUTS2 framework to achieve its function
The STRUTS2 framework uses the Commons-fileupload component by default to complete file upload functionality.
? Using the STRUTS2 framework for file upload, simply define a java.io.File type in action
and set up a setter method for the method name that corresponds to the parameter name.
? The client uploads the file, and the STRUTS2 framework automatically saves it in a temporary file, encapsulated as
The Java.io.File class object.
If you also want the uploaded file name and file type, you must set the member in action according to the following naming convention:
The variable that holds the file name: paraname+ "filename";
Save variable for file type: paraname+ "ContentType" (be sure to follow its naming convention)
If you have uploaded multiple files using the same parameter name, you can simply change the members in the action to a generic list:
Private List<file> Paraname;
Private List<string> Paranamefilename;
Private List<string> Paranamecontenttype;
The following examples illustrate
The above is the basic implementation code of JSP page
Below comes the Struts.xml file configuration
Then come to our fileaction page to write the upload method
And then come to the Struts.xml file to configure the file blocker
Special attention is also written on the above, is the FileUpload interceptor must be written in the Defaultstack stack before!!!
OK,, after the above steps will be able to complete the file upload ....
Let's take a look at the file download-------
The basic idea is that we use a list to display the name of the file, then click on the filename to complete the download
Write a JSP page first
Configuring in the Struts.xml file
Then come to Fileaction to write the list method
This will get the file name,,, from the success is not far away ...
And then into the struts.xml.
Come again to fileaction write download and getInputStream ()
So it's O.
Special attention is paid to the following:
In the download method of Fileaction, set ContentLength, Contentdisposition,
··· ContentType, and a getInputStream () method, which returns a
an input stream;
ContentType is the MIME type of the file;
ContentLength is the length of the file;
CONTENTDISPOSITION Specifies how clients are handled;
··· Configure the Fileaction in the Struts.xml file:
<result name="Success " Type="stream">
<param name="ContentType">${contentType}</param>
<param name="ContentLength">${contentLength}</param>
<param name="contentdisposition">${contentDisposition}</param>
</result>
OK,, the above is the file upload download Basic method ...
Upload and download files with Struts2