http://www.programering.com/a/MTNxQTNwATg.html
multipart/form-data
">Struts2 file upload, Download
struts2A file upload, Download
Struts2 can achieve upload, download is the component of commons-fileupload-1.2.2.jar and commons-io-2.0.1.jar based on.
The enctype property of the form elements,
The form specified in the enctype property is the form data coding, this property has the following 3 values:
application/x-www-form-urlencoded: This is the default encoding, it deals only with the value attribute of a form field values, the coding form will form field values into URL code.
multipart/form-data: This code to binary stream way to deal with the form data, this code will file the domain specified file content is encapsulated into the request parameters.
text/plain: This encoding mode when the action property of the form of the mailto:URL form is more convenient, this way is applicable to directly through the form to send email.
Sequence:
According to the example analysis:
The JSP page:
UploadAction :
public class UploadAction extends ActionSupport {
private String file;//The name and the JSP parameter names, (to set),
private String fileFileName; //The parameter name +FileName, it must be so. Save the file real name.
private String fileContentType; //The parameter name +ContentType, it must be so. Save the file type.
/*
* get/The set method
*/
public String execute() throws Exception {
//JSP form will form content in binary form of parameter passing to action.
//At this time, action file save the uploaded file.
//File path
System.out.println(file.getAbsolutePath());
}
}
Note:
Making making the results on the machine, I run the following:
Making making file upload, Struts2 will first temporary the file is saved, struts.multipart.saveDir used to specify the temporary file location. Struts.multipart.saveDir=null (not specified) would be saved to a location server.
Making making making configuration of struts.multipart.saveDir in two ways:
- Configuration in the struts.xml,
. - Write a sturt.properties, struts.multipart.saveDir=/temp.
struts2The default upload the maximum file size is 2M, We can modify the configuration file to change the size(in bytes)
- In thestruts.xmlConfiguration,
- In thestruts.propertiesConfiguration, struts.multipart.maxSize=10701096.
Note:
In the struts.propertiesThe configuration can be placed in the struts.xmlUsing constantTo configure.
OK, now get down to business:
Upload a single file:
public String execute() throws Exception {
//Create a folder in webRoot, used to store the file to upload, "upload".
//In fact, this document is only a mapping, really save the file upload in the Tomcat.
//The real path to obtain Tomcat upload.
String realPath = ServletActionContext
.getRequest()
.getSession()
.getServletContext()
.getRealPath("upload");
//The real path + real file name, the file copy to complete.
File theCopy = new File(realPath, fileFileName);
//The input stream into a temporary file, read data. The input, output, are relative to the program.
InputStream is = new FileInputStream(file);
//The output stream is inserted into the new file, and write data.
OutputStream os = new OutputStream(theCopy);
//A buffer is an array of bytes.
byte[] buffer = new byte[];
int length=0;
//Start copy
while ( (length=is.read(buffer0)) != -1 ) {
os.write(buffer, 0, length);
}
}
//Delete temporary files?
Multiple file upload:
Multiple file upload to upload a single file is similar, but the operation is a collection.
Examples are as follows:
jspPage:
<! - - All parameters upload must be a name, strutsThey will be packaged into a collection-- >
action:
public class UploadFileAction extends ActionSupport {
private List files;
private List filesFileName;
private List filesContentType;
//get, The set method
public String execute( ) throws Exception {
String realpath = ServletActionContext
.getRequest()
.getSession()
.getServletContext()
.getRealPath("upload");
for(int i=0; i
File copy = new File(realpath, filesFileName.get(i) );
inputStream is = new FileInputStream ( files.get(i) );
OutputStream os = new FileOutputStream (copy );
byte[] buffer = new byte[];
int length = 0;
while( ( length =is.read(buffer) ) != -1 ) {
os.write( buffer, 0, length );
}
}
return SUCCESS;
}
}
File download:
action:
public class DownloadFileAction extends ActionSupport {
private String fileName;
private InputStream downloadFile;
//The main method
public InputStream getDownloadFile() {
try {
//According to the conditions of the corresponding file download simulation
if( num == 1 )
//Will develop the file as a flow back out;
return return ServletActionContext
.getServletContext()
.getResourceAsStream("/upload/xxoo.jpg");
else
//Will develop the file as a flow back out;
return return ServletActionContext
.getServletContext()
.getResourceAsStream("/upload/jj.txt");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getFileName() {
return this.fileName;
}
public String execute() throws Exception {
return SUCCESS;
}
}
strutsTo configure
<! – The specified file upload, temporary file location- - >
<!- - Note that resultIs the type of stream - ->
<!—inputNameThe source parameters of the specified file, according to the configuration file, find getDownloadFile method to obtain the flow - ->
inputName
">downloadFile
<!—To specify a download mode, the default isinline, This way, if you can open the file browser, can directly open, but not to download (such as the.Txt file format). AppointattachmentWay, so that the user can select is open or download. - - >
name="contentDisposition">attachment;filename="xxoo.jpg"
<! – If you do not want to write the dead file, "xxoo.jpg" can be replaced${fileName},actionMust provide a get method - fileName ->
jspPage:
You can also specify a file format, buffer size, such as:
contentType
">image/jpeg
imageStream
attachment;filename="document.pdf"
bufferSize
">1024
No comments:
Post a Comment