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 {@Overridepublic 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 {@Overrideprotected 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 stubSystem.out.println("udpate....");return null;}public ActionForward delete(ActionMapping arg0, ActionForm arg1,HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {// TODO Auto-generated method stubSystem.out.println("delete....");return null;}public ActionForward find(ActionMapping arg0, ActionForm arg1,HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {// TODO Auto-generated method stubSystem.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>

转载于:https://www.cnblogs.com/xj626852095/p/3648149.html

JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction相关推荐

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

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

  2. .net fileupload批量上传可删除_【JavaWeb基础】文件上传和下载(修订版)

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 什么是文件上传? 文件上传就是把用户的信息保存起来. ...

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

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

  4. JavaWeb 文件上传和下载

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

  5. Java Web项目中遇到的文件上传与下载问题

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

  6. java web 文件上传和下载

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

  7. apache的开源工具common-fileupload实现文件上传和下载

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

  8. JavaWeb:实现文件上传与下载

    JavaWeb:实现文件上传与下载 文件上传前端处理 本模块使用到的前端Ajax库为Axio,其地址为GitHub官网. 关于文件上传 上传文件就是把客户端的文件发送给服务器端. 在常见情况(不包含文 ...

  9. Javaweb之文件上传与下载

    Javaweb之文件上传与下载 1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图 ...

最新文章

  1. WinDbg配置和使用基础
  2. 中国煤炭行业十四五投资战略与供需形势分析报告2022版
  3. Linux内核启动流程分析(一)【转】
  4. python让你再也不为文章配图与素材发愁,让高清图片占满你的硬盘! #华为云·寻找黑马程序员#
  5. iMAG移动应用快速开发平台简介
  6. 【测试】软件测试用例设计
  7. pycharm的安装及破解
  8. win10电脑忘记开机密码的解锁方法
  9. logistic回归详解
  10. Java模拟醉汉行走问题_用R模拟二维随机行走
  11. bedtools查找基因组位置的信息
  12. IPMI用户名密码忘记了怎么处理?
  13. Kafka集群搭建过程(kafka2.5+eagle)
  14. Excel数值函数(4):对指定条件的单元格求和
  15. 威学一百_威学一百app下载-威学一百官网版下载v1.0.0_MDPDA手机网
  16. python 证书-Python 发送带自签名证书的 https 请求
  17. JSON学习一(基础)
  18. 如何实现水泥窑分散点信号集中控制?
  19. stm32 关于GPIO寄存器操作
  20. 工况路普的采集与数据处理

热门文章

  1. 最新SSM完整模板(Spring+SpringMVC+MybatisPlus)
  2. layui表格checkbox选择全选样式及功能
  3. python调用oracle过程 权限不足_Python连接Oracle的一些坑以及出现原因和解决方法...
  4. 斑马打印机怎么打印二维码_万能打印机厂家是怎么改良打印机的?
  5. PHP笔记-打印99乘法表例子
  6. Oracle笔记-Timestamp类型的插入及获取(JDBC获取)
  7. Qt文件编码转换工具(二) C++判断文件编码
  8. 专业课程设计之客户与服务器程序的同步与通信机制的设计(三)数据共享和线程
  9. C/C++ OpenCV设置感兴趣区域ROI
  10. 江苏省有JAVA技能大赛,江苏省职业学校技能大赛组委会