1. 多文件上传与下载

上传下载jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head> <body> <a href="${pageContext.request.contextPath }/downFile.do">下载</a> <form action="${pageContext.request.contextPath }/UpFile.do" method="post" enctype="multipart/form-data">
上传用户:<input type="text" name="username"><br/>
上传文件1:<input type="file" name="list[0]"><br/>
上传文件2:<input type="file" name="list[1]"><br/>
<input type="submit" value="上传"> </form> </body>
</html>

actionform bean:

public class UpFileFormBean extends ActionForm {

	private String username;
private List<FormFile> list = new ArrayList();; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public FormFile getList(int index) {
return list.get(index);
}
public void setList(int index,FormFile file) {
list.add(file);
} public List<FormFile> getAll(){
return list;
} }

上传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());
FormFile file = formbean.getUpfile(); String filename = file.getFileName();
InputStream in = file.getInputStream();
FileOutputStream out = new FileOutputStream("c:\\" + filename);
int len = 0;
byte buffer[] = new byte[1024];
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
out.close();*/ //多文件上传
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("c:\\" + 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:

public class DownFileAction extends DownloadAction {

	@Override
protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1,
HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("content-disposition", "attachment;filename=1.jpg");
String downfile = request.getSession().getServletContext().getRealPath("/download/1.jpg"); //servlet
return new DownloadAction.FileStreamInfo("image/jpg", new File(downfile));
}
}

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="cn.itcast.web.formbean.UpFileFormBean"></form-bean>
</form-beans> <action-mappings> <action path="/downFile" type="cn.itcast.web.action.DownFileAction"></action> <action path="/UpFile"
type="cn.itcast.web.action.UpFileAction"
name="UpFileFormBean"
scope="request"
>
</action>
</action-mappings> <!--最大上传文件-->
<controller processorClass="org.apache.struts.action.RequestProcessor" maxFileSize="1K"></controller> </struts-config>

web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <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>
<load-on-startup>2</load-on-startup> </servlet> <servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

2. 如何实现一个action处理多个请求,  两种实现 DispatchAction 和 MappingDispatchAction

多请求jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head> <body> <html:link action="/BookAction?method=add">添加图书</html:link>
<html:link action="/BookAction?method=delete">删除图书</html:link>
<html:link action="/BookAction?method=update">修改图书</html:link>
<html:link action="/BookAction?method=find">查找图书</html:link> <br/> <br/>---------------------------------------------<br/><br/> <html:link action="/addbook">添加图书</html:link>
<html:link action="/deletebook">删除图书</html:link>
<html:link action="/updatebook">修改图书</html:link>
<html:link action="/findbook">查找图书</html:link> </body>
</html>

action 处理,两种实现DispatchAction 和 MappingDispatchAction, 配置文件不同

//DispatchAction---action  ---execute()  add 

/*
String method = mapping.getParamter();
String methodName = request.getParameter(method); //add update
*/
public class BookAction extends DispatchAction { public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { System.out.println("add....");
return null;
} public ActionForward update(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
System.out.println("udpate....");
return null;
} public ActionForward delete(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
System.out.println("delete....");
return null;
} public ActionForward find(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
System.out.println("find....");
return null;
}
}
 
public class BookAction2 extends MappingDispatchAction {

	public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { System.out.println("add....");
return null;
} public ActionForward update(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub System.out.println("udpate....");
return null;
} public ActionForward delete(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub System.out.println("delete....");
return null;
} public ActionForward find(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
System.out.println("find....");
return null;
}

web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--struts配置文件可以添加多个配置文件struts-config2.xml-->
<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,/WEB-INF/struts-config2.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup> </servlet> <servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts-config.xml 配置文件1

<?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>
<action-mappings>
<action path="/BookAction"
type="cn.itcast.web.action.BookAction"
parameter="method"> <!-- 告诉struts,要调用的方法名称是通过什么参数带过来的 -->
</action>
</action-mappings>
</struts-config>

struts-config2.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> <action-mappings>
<action path="/addbook" type="cn.itcast.web.action.BookAction2" parameter="add"/>
<action path="/updatebook" type="cn.itcast.web.action.BookAction2" parameter="update"/>
<action path="/findbook" type="cn.itcast.web.action.BookAction2" parameter="find"/>
<action path="/deletebook" type="cn.itcast.web.action.BookAction2" parameter="delete"/>
</action-mappings> </struts-config>
												

JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction的更多相关文章

  1. JavaWeb学习总结——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  2. javaWeb中,文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  3. JavaWeb中的文件上传和下载功能的实现

    导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...

  4. javaWeb学习总结——文件上传、下载

    目录 1.文件上传环境搭建 2.文件上传代码实现 3.关于下载 @ 嘿,熊dei,你不得不知道在Web开发中,文件上传和下载功能是非常常用的功能,关于文件上传,浏览器上传[文件以流的形式传输]--&g ...

  5. Struts1.3——文件上传和下载

    1.Struts文件上传 在Web开发中,会经常涉及到文件的上传和下载,比如在注册账户的时候,我们需要上传自己的头像等. 我们可以利用Struts很方便地实现文件的上传. 1.1 开发步骤 现在,假设 ...

  6. JavaWeb总结(十)—文件上传和下载

    一.文件的上传 1.文件的基本上传 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择 ...

  7. (转载)JavaWeb学习总结(五十)——文件上传和下载

    源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...

  8. JavaWeb学习总结,文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  9. JavaWeb学习总结(五十)——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

随机推荐

  1. 联想打字必须按FN+数字-fn打字

    对于联想G40.14英寸系列的本本,好多时候无意间可能把数字键锁定了. 这时候要做的是:打开运行--输入OSK--打开虚拟屏幕键盘.这时候可以找到 选项---打开数字键盘. 有时候某些电脑上没有NUM ...

  2. Windows Server 2008及以上系统磁盘无法查看(About UAC and ACE)

    在windows Server2008及以上系統,如果UAC Enabled,ACE列表中不會包含Administrators成員的SID,所以即使你是administrators的成員,也無法訪問D ...

  3. 使用ADO如何获得SQLSERVER 2K的数据库名的列表

    打开数据库连接_ConnectionPtr m_pConn;_RecordsetPtr m_pRs;m_pConn.CreateInstance(__uuidof(Connection));m_pRs ...

  4. 七、H5 直播视频播放

    HTML5实现视频直播功能思路详解_html5教程技巧_脚本之家 https://m.jb51.net/html5/587215.html 七.H5 直播视频播放 移动端iOS和 Android 都天 ...

  5. 关于websocket

    一句话总结: websocket可以说是基于HTTP但有有所进化的一个介于应用层和传输层的接口抽象,不是协议. 1 需要基于HTTP进行3次握手,4次挥手(在握手期间建立websocket连接,不再通 ...

  6. Virtualbox报错------>make sure the kernel module has been loaded successfully

    错误描述 很久没有用virtualbox了,今天打算在virtualbox上安装一个Ubuntu系统的时候,新建好Ubuntu后启动的时候,直接报错: Cannot access the kernel ...

  7. android自定义控件(一)MeasureSpec 与 ListView.onMeasure

    A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec rep ...

  8. Python 模块之 time & datetime

    Python 中提供了对时间日期的多种多样的处理方式,主要是在有 time 和 datetime 两个模块. time 在 Python 文档里,time 是归类在 Generic Operating ...

  9. Angular学习笔记—RxJS与Observable(转载)

    1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...

  10. Redis三(List操作)

    List操作 redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name,values) 1 2 3 4 5 6 7 8 # 在name对应的list中添加元 ...