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




 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-07-31
  • 2022-01-16
  • 2021-12-14
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2021-12-29
  • 2021-11-29
  • 2021-11-19
  • 2021-10-18
  • 2022-02-04
相关资源
相似解决方案