struts2--file upload and download

File upload and download:

Upload: Upload the local file to the server.

struts2 file upload is a further encapsulation based on fileUpload;

The operation of struts2 file upload is in the interceptor of <interceptor-ref name="fileUpload"/>;

Three requirements for file upload: 1), the form is submitted as post; 2), the file upload item in the form, the file upload item must have the name attribute; 3), the enctype="multipart/form-data" in the form

Steps to upload:

Declare three variables and generate the set method of the variables

//Uploaded file (is the value of the name in the file upload item)

private File upload;

//Declare the upload file name naming convention The value of the name in the file upload item FileName

private String uploadFileName;

//Declare the mime type of the format of the uploaded file, each file format corresponds to the type of mime type, and different processing is performed for different types

private String uploadContentType;

get and set methods for setting variables;

Get the path of the uploaded file, create a file in the server, and copy the uploaded file to the file on the server

// get the path of the uploaded file

String path = ServletActionContext.getServletContext().getRealPath("/img");

//Copy the uploaded file to a file on the server

//create file in server

File file = new File(path+"/"+uploadFileName);

//copy file

FileUtils.copyFile(upload, file);

Problems with uploading files:

It has a size limit, typically 2M, which requires setting changes in constants:

<constant name="struts.multipart.maxSize" value="20000000"></constant>

 

Multiple file upload:

It is similar to the single-file upload method, except that the variables are generated in the form of an array; when the server creates a file, it must be traversed first, and then uploaded to the server one by one:

// get the path of the uploaded file

String path = ServletActionContext.getServletContext().getRealPath("/img");

for(int i=0;i<uploadimg.length;i++) {

// get each file

File file = new File(path+"/"+uploadimgFileName[i]);

//copy

FileUtils.copyFile(uploadimg[i], file);

}

 

Download of files:

Copy the file from the server to the local.

 

Configure in the action configuration result

<result name="success" type="stream">

<param name="contentDisposition">attachment;filename=${fileName}</param>

<param name="inputStream">${inputStream}</param>

</result>

(2) Pass fileName and inputStream through action

action:

private String name = "中文.jpg";

//Provide the get method of fileName and inputStream

public String getFileName() {

//encode the file name

//Distinguish different browser User-Agent

String agent = ServletActionContext.getRequest().getHeader("User-Agent");

if (agent.contains("Firefox")) { // Firefox

try {

name = "=?UTF-8?B?"

+ new BASE64Encoder().encode(name.getBytes("utf-8"))

+ "?=";

} catch (UnsupportedEncodingException e) {

e.printStackTrace ();

}

} else { // IE and other browsers

try {

name = URLEncoder.encode(name, "utf-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace ();

}

}

return name;

}

 

public InputStream getInputStream() {

// get the full path of the downloaded file

String url = ServletActionContext.getServletContext().getRealPath("/img/"+name);

//Get the input stream of the downloaded file

try {

InputStream in = new FileInputStream(url);

return in;

} catch (FileNotFoundException e) {

e.printStackTrace ();

}

return null;

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326519609&siteId=291194637