文件上传和下载在很多web开发中用到,现在,我就将我整理的利用strut2实现文件上传和下载的功能与大家共享一下,希望有用。
1、文件上传:
1)action中代码:
package com.xxyd.web.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 处理文件上传的Action
* @author dove
*/
public class UploadAction extends ActionSupport {
/** 代表上传的文件内容的对象 */
private File upload;
/** Struts2约定的代表上传的文件的名 */
private String uploadFileName;
/** Struts2约定的代表上传文件的内容类型(MIME) */
private String uploadContentType;
public String execute() throws Exception{
System.out.println("文件的名:" + uploadFileName);
System.out.println("不要用upload.getName()来获取文件名,这个是临时名:" + upload.getName());
System.out.println("文件的内容类型:" + uploadContentType);
//使用IO流来操作upload属性
//File destPath = new File("d:/"); //服务端存放文件的目录
//如果要存放到web服务器中本项目的某个目录下
//根据服务器的文件保存地址和原文件名创建目录文件全路径
String destPath = ServletActionContext.getServletContext().getRealPath("/uploads");
File dest = new File(destPath, uploadFileName); //服务器的文件
FileUtils.copyFile(upload, dest);//完成了文件的拷贝工作
return "succ";
}
public String getSummery() {
return summery;
}
public void setSummery(String summery) {
this.summery = summery;
}
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;
}
}
2)struts.xml中的配置
<action name="upload" class="com.xxyd.web.action.UploadAction">
<!-- 可以更改fileUpload拦截器的属性值来限定上传文件的内容类型,上传文件的大小 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg</param>
<param name="fileUpload.maximumSize">102400</param>
</interceptor-ref>
<result name="succ">/succ.jsp</result>
<result name="input">/index.jsp</result>
</action>
3)html代码:
<html>
<head>
<title>Struts2文件上传示例</title>
</head>
<body>
<h3>Struts2文件上传示例</h3><hr/>
<s:fielderror/>
<form action="upload.action" method="post" enctype="multipart/form-data"><!-- 此处必须为multipart/form-data,而且必须使用post方法 -->
<table border="1" width="500">
<tr>
<td>选择文件</td>
<td><input type="file" name="upload" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value='提交' /></td>
</tr>
</table>
</form>
</body>
</html>
此时,便可以实现文件上传了。
2、文件下载:
1)action中代码:
package com.xxyd.web.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
/**
* 处理文件下载的Action类
* @author dove
*/
public class DownLoadAction {
private String basePath = ServletActionContext.getServletContext().getRealPath("/uploads");
private String fileName;
public String execute(){
return "succ";
}
public InputStream getInputStream() throws FileNotFoundException{
return new FileInputStream(new File(basePath, fileName));
}
public String getFileName() throws UnsupportedEncodingException {
return new String(fileName.getBytes(), "ISO-8859-1");
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
2)struts.xml中的配置:
<!-- 文件下载 -->
<action name="download" class="com.xxyd.web.action.DownLoadAction">
<result name="succ" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">8192</param>
</result>
</action>
3)html页面代码:
<html>
<head>
<title>Struts2文件下载功能实例</title>
</head>
<body>
<a href="download.action?fileName=sunset.jpg">下载sunset.jpg</a><br/>
</body>
</html>
此时,便可以实现文件的下载功能了。