Struts2 上传和下载功能
DowloadAction Action类
package action;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DowloadAction extends ActionSupport {
//读取下载文件的目录
private String inputPath;
//下载文件的文件名
private String fileName;
//读取下载文件的输入流
private InputStream inputStream;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() {
//从下载里面获取指定的文件名 upload /123.jpg
String path=ServletActionContext.getServletContext().getRealPath(inputPath);
String loaction=path+"/"+fileName;
BufferedInputStream bis=null;
try {
FileInputStream fis=new FileInputStream(loaction);
bis=new BufferedInputStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bis;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
UploadAction Action类
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private File upload;
//获取提交的文件类型
private String uploadContentType;
//封装上传文件名称
private String uploadFileName;
//获取文件上传的路径 upload文件夹
private String savePath;
@Override
public String execute() throws Exception {
//空杯子
byte[] buff=new byte[1000];
//文件保存文件
String path=ServletActionContext.getServletContext().getRealPath(savePath);
System.out.println(path);
String loaction=path+"/"+uploadFileName;
//空瓶子
FileOutputStream fos=new FileOutputStream(loaction);
//循环读取文件,并且写入到fos
//大可乐瓶子
FileInputStream fis=new FileInputStream(upload);
int lenght=fis.read(buff);
while(lenght>0){
fos.write(buff, 0, lenght);
lenght=fis.read(buff);
}
//关闭资源
fis.close();
fos.close();
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
配置 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="action.UploadAction">
<param name="savePath">upload</param>
<result name="success">uploadSuccess.jsp</result>
</action>
<action name="download" class="action.DowloadAction">
<param name="inputPath">upload</param>
<result name="success" type="stream">
<param name="contentType">image/pjpeg</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
upload.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="upload.action" method="post"
enctype="multipart/form-data">
用户名 <input type="text" name="username" /> <br /> 图片 <input
type="file" name="upload" /> <br /> <input type="submit" value="提交" />
</form>
</body>
</html>
uploadSuccess.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<h4>
上传成功,你传的文件名称是:
<s:property value="uploadFileName" />
</h4>
<h4>
<s:property value="username" />
传的文件类型是
<s:property value="uploadContentType" />
</h4>
</body>
</html>
index.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<h4>This is Index Page</h4>
<a href="download.action?fileName=123.jpg">下载123.jpg</a>
</body>
</html>
效果图: