Struts2文件上传和下载
1. 文件上传
1.1单文件上传
Action类
public class FileUploadAction extends ActionSupport {
//具体上传文件的 引用 , 指向临时目录中的临时文件
private File upload;
//上传文件的类型, ContentType 固定的写法
private String uploadContentType;
// 上传文件的名字 ,FileName 固定的写法
private String uploadFileName;
//上传文件的路径
private String savePath;
@Override
public String execute() throws Exception {
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(getUpload());
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
int length=fis.read(buffer);
while (length>0){
fos.write(buffer,0,length);
length=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
return SUCCESS;
}
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() {
//getRealPath()把相对路径转化为绝对路径
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
Struts.xml配置
<action name="uploadAction" class="cn.bdqn.file.FileUploadAction">
<param name="savePath">/upload</param>
<result name="success">/stuts2Upload/strutsupload.jsp</result>
</action>
fileupload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2文件上传</title>
</head>
<body>
<s:form action="uploadAction" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>
strutsupload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2文件上传页面</title>
</head>
<body>
文件是: <s:property value="uploadFileName"></s:property><br/>
文件类型:<s:property value="uploadContentType"></s:property><br/>
</body>
</html>
错误问题
1.2多文件上传
Action类
public class MoreFileUploadAction extends ActionSupport {
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
private String savePath;
@Override
public String execute() throws Exception {
byte[] buffer=new byte[1024];
File file=new File(getSavePath());
if(!file.exists()){
file.mkdirs();
}
int index=0;
for (File item:upload) {
FileInputStream fis=new FileInputStream(item);
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName().get(index++));
int length=fis.read(buffer);
while (length>0){
fos.write(buffer,0,length);
length=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
}
return SUCCESS;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
Struts.xml配置
<action name="moreuploadAction" class="cn.bdqn.file.MoreFileUploadAction">
<param name="savePath">/upload</param>
<result name="success">/moreupload/morestrutsupload.jsp</result>
</action>
morestrutsfileupload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Struts2文件上传</title>
</head>
<body>
<s:form action="moreuploadAction" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件"/><br/>
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>
morestrutsupload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Struts2多文件上传</title>
</head>
<body>
<s:iterator value="uploadFileName">
文件:<s:property/> <br/>
</s:iterator>
<s:iterator value="uploadContentType">
文件类型:<s:property/> <br/>
</s:iterator>
</body>
</html>
2. 文件下载
Action类
public class FileDownAction extends ActionSupport {
//文件路径
private String inputPath;
//提交过来的name
private String fileName;
private InputStream inputStream;
//提交过来的file的MIME类型
private String contentType;
@Override
public String execute() throws Exception {
return SUCCESS;
}
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;
}
//返回inputstream,文件下载关键方法
public InputStream getInputStream() throws Exception {
String path= ServletActionContext.getServletContext().getRealPath(inputPath);
return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}
Struts.xml配置
<action name="filedownAction" class="cn.bdqn.file.FileDownAction">
<!-- 设置文件路径的参数,传到action类文件中去 -->
<param name="inputPath">/upload</param>
<!-- 下载文件类型定义,即定义为“stream” -->
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<!-- 下载文件输出流定义 -->
<!-- 这里的inputName元素所对应的value值downloadFile,在action中一定要有对应的getDownloadFile()方法 -->
<param name="inputName">inputStream</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param><!--设置响应的HTTP头信息中的Content-Disposition参数的值-->
<!-- 下载缓冲区的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
strutsfiledown.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Struts2文件下载</title>
</head>
<body>
<a href="filedownAction.action?fileName=1.jpg">文件下载</a></body>
</html>