1. 多文件上传与下载
上传下载jsp:
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 配置文件
web.xml 配置文件
2. 如何实现一个action处理多个请求, 两种实现 DispatchAction 和 MappingDispatchAction多请求jsp:
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 配置文件
struts-config.xml 配置文件1
struts-config2.xml