Today, I learned the upload and download of struts2 files. The following are the specific steps and code (the download of this example is to download the last uploaded file): The
first step: jsp page, index.jsp:
<s:form action="upload" method="post" theme="simple " enctype="multipart/form-data">
<table align="center" width="50%" border="1">
<tr>
<td>Select upload file</td>
<td id="more" >
<s:file name="upload"></s:file>
</td>
</tr>
<tr>
<td><s:submit type="button" value="Submit" onclick="return checkf ()"/></td>
<td><s:reset value="reset"></s:
<a href="<s:url value='download.action'></s:url>">下载</a>
</td>
</tr>
</table>
</s:form>
第二部:写action代码:
package com.hp.lwq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UpDownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L; private File upload;
//Upload is required
private String uploadFileName;
private String uploadContentType;
private String savePath;
//下载时需要的属性
private String downloadFileName;;
private InputStream content;
private String path;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getDownloadFileName() {
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getContent() throws Exception{
//从session中获取上次上传的文件
String downName=(String) ActionContext.getContext().getSession().get("uploadFileName");
//获取文件的路径
String path = ServletActionContext.getServletContext().getRealPath(
"/uploads/"+downName);
//根据路径找到文件
File file = new File(path);
this.downloadFileName = file.getName();
this.downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
content = new FileInputStream(file);
return content;
}
public void setContent(InputStream content) {
this.content = content;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//================================================================
//上传
public String upload() throws Exception{
//The server-side detailed path obtained by obtaining the Servlet context
String realPath = ServletActionContext.getServletContext().getRealPath(
savePath);
/*//Save the storage path in the request
ActionContext.getContext() .put("realPath", realPath);*/
//Create a directory based on the obtained path
File path = new File(realPath);
if (!path.exists()) {
path.mkdirs();
}
//Use input Output stream read and write files
//Using the input stream to read the file according to the file attribute of the action
FileInputStream is = new FileInputStream(upload);
//Create an output stream according to the file name attribute of the action and the server path
FileOutputStream os = new FileOutputStream(realPath + " /"
+ uploadFileName);
//Put the newly uploaded file into the session by name
ActionContext.getContext().getSession().put("uploadFileName", uploadFileName);
//Create byte array, receive stream and write to file (8k here)
byte buffer[] = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
//close the stream
os.close();
is.close();
return SUCCESS;
}
//Download
public String download() {
return SUCCESS;
}
}
Part 3: Configure struts.xml file:
<action name="upload" class="com.hp.lwq.action.UpDownloadAction" method="upload" >
<param name="savePath">/uploads</param>
<result name="success">/result.jsp< /result>
<result name="input">/upload.jsp</result>
</action>
<action name="download" class="com.hp.lwq.action.UpDownloadAction" method="download">
<param name="path">/downloads</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="bufferSize">8192</param>
<param name="inputName">content</param>
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
</result>
<result name="error" >/error.jsp</result>
</action>
Note: The uploaded directory is in the webapps/uploads folder under the tomcat server.
The downloaded directory is the directory where the downloaded things are stored with the browser.
Use Struts2 to complete file upload and download functions
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326780376&siteId=291194637
Recommended
Ranking