struts2文件上传和下载
1.多文件上传
1.编写filesuploads.jsp和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="uploads" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="newbeedaly"/><br/>
文件2:<input type="file" name="newbeedaly"/><br/>
文件3:<input type="file" name="newbeedaly"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功!
</body>
</html>
2.配置struts.xml文件
<action name="uploads" class="com.newbeedaly.action.FilesUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/filesupload.jsp</result>
</action>
3.编写FilesUploadAction类
package com.newbeedaly.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FilesUploadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private File[] newbeedaly; // 文件
private String[] newbeedalyFileName; // 文件名
private String[] newbeedalyContentType; // 文件类型
public File[] getnewbeedaly() {
return newbeedaly;
}
public void setnewbeedaly(File[] newbeedaly) {
this.newbeedaly = newbeedaly;
}
public String[] getnewbeedalyFileName() {
return newbeedalyFileName;
}
public void setnewbeedalyFileName(String[] newbeedalyFileName) {
this.newbeedalyFileName = newbeedalyFileName;
}
public String[] getnewbeedalyContentType() {
return newbeedalyContentType;
}
public void setnewbeedalyContentType(String[] newbeedalyContentType) {
this.newbeedalyContentType = newbeedalyContentType;
}
@Override
public String execute() throws Exception {
for(int i=0;i<newbeedaly.length;i++){
System.out.println("文件名:"+newbeedalyFileName[i]);
System.out.println("文件类型:"+newbeedalyContentType[i]);
String filePath="C:/"+newbeedalyFileName[i];
File saveFile=new File(filePath);
FileUtils.copyFile(newbeedaly[i], saveFile);
}
return SUCCESS;
}
}
2.文件下载
- 编写fileDownload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="download">文件下载</a>
</body>
</html>
2.配置struts.xml文件
<action name="download" class="com.newbeedaly.action.FileDownloadAction">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
3.编写类
package com.newbeedaly.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String fileName;
public String getFileName() throws Exception{
fileName=new String(fileName.getBytes(),"ISO8859-1");
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream()throws Exception{
File file=new File("C:/美女1.jpg");
this.fileName="美女1号";
return new FileInputStream(file);
}
}