struts多文件上传以及文件下载源码
这里使用Struts1.3.8举例,版本很老了,但是基本的思路是一样的,新的Struts做了一些改良
多文件上传
jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<form action="${pageContext.request.contextPath }/UpFile.do" method="post" enctype="multipart/form-data">
上传用户:<input type="text" name="username"><br>
<!-- 使用UpFileFormBean的upfiles保存 -->
上传文件1:<input type="file" name="upfiles[0]"><br>
上传文件2:<input type="file" name="upfiles[1]"><br>
<input type="submit" value="上传">
</form><br>
<a href="${pageContext.request.contextPath }/DownFile.do">下载oracle.gif</a>
</body>
</html>
保存提交数据的formbean
public class UpFileFormBean extends ActionForm{
private String username;
private List<FormFile> upfiles=new ArrayList<>();//这里最好使用list来保存,使用数组([])比较麻烦
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public FormFile getUpfiles(int index) {
return upfiles.get(index);
}
public void setUpfiles(int index,FormFile file) {
upfiles.add(file);
}
public List<FormFile> getAll(){
return upfiles;
}
}
图片上传action
public class UpFileAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
UpFileFormBean formBean=(UpFileFormBean) form;
System.out.println("上传用户:"+formBean.getUsername());
List<FormFile> all=formBean.getAll();
System.out.println(all.size());
for(FormFile formFile:all){
String filename= formFile.getFileName();
InputStream in=formFile.getInputStream();
FileOutputStream out=new FileOutputStream("D:\\"+filename);
int len=0;
byte buffer[]=new byte[1024];
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
}
return super.execute(mapping, form, request, response);
}
}
图片下载action,这里继承了Struts的DownloadAction用来下载
public class DownFileAction extends DownloadAction {
@Override
protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//通知浏览器以下载形式访问
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=oracle.gif");
response.setHeader("content-type", "zip");
String path=request.getSession()
.getServletContext()
.getRealPath("/download/oracle.gif");
return new DownloadAction.FileStreamInfo("image/gif", new File(path));
}
}
WEB-INF下的struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="UpFileFormBean" type="com.wz.formbean.UpFileFormBean">
</form-bean>
</form-beans>
<action-mappings>
<action path="/UpFile"
type="com.wz.action.UpFileAction"
name="UpFileFormBean"
scope="request">
</action>
<action path="/DownFile"
type="com.wz.action.DownFileAction"
>
</action>
</action-mappings>
<!-- 此处设置文件上传大小 可以以K,M,G为单位,默认250M
如果上传文件超出了最大值,Struts不会把上传数据封装到FormFile中,也就是
FormFile=null,在程序中根据它是否为空,提示用户文件是否超出大小
-->
<controller processorClass="org.apache.struts.action.RequestProcessor" maxFileSize="1M"></controller>
</struts-config>
WEB-INF下的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>strutsFileUpAndDown</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts的核心servlet -->
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<!-- 设置该servlet启动时运行 -->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>