This example for you to share the STRUTS2 implementation of the file upload download the specific implementation code for your reference, the specific contents are as follows
First, File upload
Struts submitted the file component upload,
Front Desk:
1), submit the way post
2), form type Multipart/form-data
3), Input Type=file
Backstage:
Apache-provided fileupload components
Core classes:
the factory of Fileitemfactory Fileitem
Core classes for file uploads in the Servletfileupload servlet
Fileitem encapsulates information for uploaded form file entries
In short file upload, deal with more trouble
Struts File Upload
The file upload interceptor helped us out of the evening. File Upload function
<interceptor
name= "FileUpload"
class= "Org.apache.structs2.interceptor.FileUploadInterceptor"/>
Upload.xml
<struts>
<package name= "Upload_" extends= "Struts-default" >
<!--Note: The name of the action cannot be used with the keyword " FileUpload "--> <action name=" fileuploadaction "class=" Cn.itcast.e_fileupload
. FileUpload ">
<!--limit the types of files that run uploads-->
<interceptor-ref name=" Defaultstack ">
<!-- Limit the extension of running files-->
<param name= "fileupload.allowedextensions" >txt,jpg,jar</param>
<!-- Restrictions on the type of operation "with the above use, take the intersection"
<param name= "fileupload.allowedtypes" >text/plain</param>
-->
</interceptor-ref>
<result name= "Success" >/e/success.jsp</result>
<!--configuration Error View-- >
<result name= "input" >/e/error.jsp</result>
</action>
</package>
</struts>
upload.jsp
<body>
<form action= "${pagecontext.request.contextpath}/fileuploadaction" method= "POST" enctype= " Multipart/form-data ">
username: <input type=" text "name=" userName "><br/>
files: <input type=" file "Name=" File1 "><br/>
<input type=" Submit "value=" Upload ">
</form>
</body>
error.jsp
<body>
error.jsp<br/>
<!--View all error messages generated by the Struts framework during run time-->
<%@ taglib uri= "/ Struts-tags "prefix=" s "%>
<s:fielderror></s:fielderror>
</body>
success.jsp
<body>
success.jsp
</body>
Core code
FileUpload. Class
public class FileUpload extends Actionsupport {
//corresponding form: <input type= "file" name= "File1" >
Private file file1;
FileName
private String file1filename;
The type of file (MIME)
private String file1contenttype;
public void SetFile1 (File file1) {
this.file1 = file1;
}
public void Setfile1filename (String file1filename) {
this.file1filename = file1filename;
}
public void Setfile1contenttype (String file1contenttype) {
this.file1contenttype = File1contenttype;
}
@Override public
String execute () throws Exception {
/****** get the uploaded file, process ******///upload the file to the
upload directory
//Get the uploaded directory path
String path = Servletactioncontext.getservletcontext (). Getrealpath ("/upload");
Creates a target file object, file
destfile = new file (path,file1filename);
Copy the uploaded file to the target file
fileutils.copyfile (File1, destfile);
Return SUCCESS
}
}
File Upload processing details
A. File size limits
Structs default supported file Upload Max is 2M, modified by constant:
<!--4. The maximum size of the upload file to modify is 30M-->
<constant name= "struts.multipart.maxSize" value= "31457280"/>
B. Limit the allowable types of uploaded files
Requirements: Files that allow only txt/jpg suffixes
Interceptors: Injecting parameters to limit file upload types
<!--limit the types of files that are uploaded-->
<interceptor-ref name= "Defaultstack" >
<!--restrictions on running files extension-->
< param name= "fileupload.allowedextensions" >txt,jpg,jar</param>
<!--limit the type of operation "with the above, take the intersection"
< param name= "fileupload.allowedtypes" >text/plain</param>
-->
</interceptor-ref>
Second , the download of documents
struts file Download, 2 ways:
Mode 1: write the byte stream data to the browser through the response object; Set the response header for the download
Mode 2:the way struts
Struts File Download:
Copy Code code as follows:
<result-type name= "Stream" class= "Org.apache.struts2.dispatcher.StreamResult"/>
First notice create a new upload folder in the Webroot directory, and put the file you want to provide the download into the folder,
Upload.xml
<action name= "down_*" class= "Cn.itcast.e_fileupload". Downaction "method=" {1} ">
<!--list show-->
<result name=" list ">/e/list.jsp</result>
<!--download operation-->
<result name= "Download" type= "stream" >
<!--the type of file to run the download: specified as all binary file types-->
<param name= "ContentType" >application/octet-stream</param>
<!--corresponds to the property in action: Returns the properties of the stream " is actually Getattrinputstream () "-->
<param name=" InputName ">attrInputStream</param>
<!--download header, Includes: browser-displayed filename-->
<param name= "Contentdisposition" >attachment;filename=${downfilename}</param >
<!--buffer size settings-->
<param name= "buffersize" >1024</param>
</result>
</action>
list.jsp
<body>
<table border= "1" align= "center" >
<tr>
<td> number </td>
<TD > filename </td>
<td> operation </td>
</tr>
<% @taglib uri= "http://java.sun.com/jsp/ Jstl/core "prefix=" C "%>
<c:foreach var=" FileName "items=" ${filenames} "varstatus=" vs ">
<tr>
<td>${vs.count}</td>
<td>${filename}</td>
<td>
<!-- Build a URL-->
<c:url var= "url" value= "Down_down" >
<c:param name= "FileName" value= "${filename}" ></c:param>
</c:url>
<a href= "${url}" > Download </a>
</td>
</tr >
</c:forEach>
</table>
</body>
Downaction
/** * File Download * 1. Displays a list of all files to download * 2. File Download */public class Downaction extends Actionsupport {/*************1. Displays a list of all files to download *********************/public String list () throws Exception {//Get upload directory path String path = Servletactioncontext.getservletcontext (). Getrealpath ("
/upload ");
Directory Object file = new file (path);
Get the filename of all files to download string[] FileNames = File.list ();
Save Actioncontext AC = Actioncontext.getcontext ();
Get the Map (the second way) that represents request Map<string,object> request= (map<string, object>) ac.get ("request");
Request.put ("FileNames", fileNames);
return "List"; }/*************2. File download *********************///1.
Gets the filename of the file to be downloaded private String filename; public void Setfilename (String fileName) {//handling an incoming parameter problem (get commit) try {fileName = new String (Filename.getbytes ("ISO8
859-1 ")," UTF-8 ");
catch (Unsupportedencodingexception e) {throw new RuntimeException (e);
///This.filename The processed file name, assigning value = FileName; }//2. Download a submitted business method(Configure return stream in Struts.xml) public String down () throws Exception {returns "Download"; }//3. The method that returns the file stream is public InputStream Getattrinputstream () {return Servletactioncontext.getservletcontext ().
getResourceAsStream ("/upload/" + fileName); }//4. Download the displayed filename (the file name that the browser displays) public String getdownfilename () {//need to encode the Chinese try {filename = urlencoder.encode (filename, UT
F-8 ");
catch (Unsupportedencodingexception e) {throw new RuntimeException (e);
return fileName;
}
}
The above is the entire content of this article, I hope to help you learn.