Struts2 Upload in Download technical analysis

Source: Internet
Author: User

1. Upload: Request--------"Struts2 FileUpload Interceptor to parse the file to be uploaded, get the corresponding attribute data injected into the action-------------" action to define the member properties and the Set method, Used to inject attribute values. The FileUpload interceptor part naming requires the source code as follows:

if (Files! = null && files.length > 0) {
list<file> acceptedfiles = new arraylist<file> (files.length);
list<string> acceptedcontenttypes = new arraylist<string> (files.length);
list<string> acceptedfilenames = new arraylist<string> (files.length);
String ContentTypeName = inputname + "ContentType";
String filenamename = inputname + "FileName";

So file upload Action member property to canonical name three attributes, are: 1. The file property name used in the document upload in the form is the same as the name of the filename in the action, which is assumed to be the attribute used in x;2.action to represent the name of the uploaded file, which must be named variable x+ "filename", no Chinese garbled (The framework has been resolved); the name of the attribute in 3.action that specifies the file MIME type must be named variable x+ "ContentType", as shown in the following example:

Front page:

<input type= "file" name= "Upload" >

Background action:

The file to upload
Private File upload;
The name of the file (can be in Chinese, garbled has been resolved)
Private String Uploadfilename;
The MIME type of the file
Private String Uploadcontenttype;

Note: The following is the set method for these properties, which is used by the interceptor to inject property values into the action, the file upload default size 2M, you can set the constant change size in Struts2.xml

<constant name= "struts.mutipart.maxSize" value= "2097152" ></constant>

Example code:

Public String Save () throws ioexception{
Do file upload, indicating that the user has selected the uploaded file
if (uploadfilename! = null) {

Deal with the name of the file and avoid duplicate file names
String uuidname = Uploadutils.getuuidname (uploadfilename);
String Path = "e:\\app\\apache-tomcat-7.0.70\\upload\\";
Create a File object
File File = new file (path+uuidname);
Simple Way
Fileutils.copyfile (upload, file);

Save the path of the uploaded file to the Customer table
Customer.setfilepath (Path+uuidname);
}

Save the customer success
Customerservice.save (customer);
return "Save";
}

2. Download

File download action must provide a return value of type InputStream get method, by business, the foreground page will come to the file storage address filepath and filenames filename, in order to solve the download box in different browser kernel garbled problem, The browser kernel needs to be judged and then the corresponding garbled processing, the corresponding code is as follows;

Determine browser kernel code

public boolean ismsbrowser (HttpServletRequest request) {

String[] Iebrowsersignals = {"MSIE", "Trident", "Edge"};

String useragent = Request.getheader ("user-agent");

for (String signal:iebrowsersignals) {

if (Useragent.contains (signal)) {

return true;

}

}

return false;

}

Parsing Chinese Code

Public String GetFileName () {
try {
HttpServletRequest request = Servletactioncontext.getrequest ();
If the IE kernel uses urlencoder
if (Ismsbrowser (request)) {

FileName = Urlencoder.encode ("Supplier Qualification", "UTF-8");

}else{//if it is google, Firefox resolves to iso-8859-1

FileName = new String ("Supplier Qualification". GetBytes ("UTF-8"), "iso-8859-1");

}
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return fileName;
}

A method that must provide a get type with a return type of InputStream, assuming GetXXX (), in which the encoding needs to be processed again

Public InputStream getXxx () throws Exception {

HttpServletRequest request = Servletactioncontext.getrequest ();
if (Ismsbrowser (request)) {

FileName = Urlencoder.encode ("Supplier Qualification", "UTF-8");

}else{//if it is google, Firefox resolves to iso-8859-1

FileName = new String ("Supplier Qualification". GetBytes ("UTF-8"), "iso-8859-1");

}
File File = new file (filepath);
return new FileInputStream (file);
}

The Struts.xml is configured as follows

<!--configuration file Download-
<action name= "filedownloadaction" class= "Filedownloadaction" >
<result name= "Success" type= "Stream" >
<param name= "ContentType" >image/jpeg;</param>
<param name= "contentdisposition" >attachment;filename= "${filename}.jpg" </param>
<param name= "InputName" >xxx</param>//note that the name should be the same as the Stream property defined in action
<param name= "BufferSize" >81920</param>
</result>
</action>

Struts2 Upload in Download technical analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.