JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction
生活随笔
收集整理的這篇文章主要介紹了
JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java回调函数使用
- 下一篇: 条款12:复制对象时勿忘其每一个部分