在Struts中使用Spring的CommonsMultipartResolver上传文件

项目是Struts + Spring的,想使用Spring的CommonsMultipartResolver完成上传文件功能。在网上查了一下,都是在Spring MVC中使用CommonsMultipartResolver的例子。自己解决了一下,方法如下:

 

最简单的,不需要配置Spring,直接在Struts的Action中写:

    public ActionForward fileUpload(ActionMapping mapping, ActionForm form,
    		HttpServletRequest request, HttpServletResponse response) throws IOException {
    	
    	CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    	commonsMultipartResolver.setDefaultEncoding("utf-8");
    	
    	if (commonsMultipartResolver.isMultipart(request)) {
    		MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart(request);

    		MultipartFile file = multipartRequest.getFile("file"); 
    		
    		File imageFile = new File(FileName);
    		
    		file.transferTo(imageFile);
    		
//    		String owner = multipartRequest.getParameter("fileOwner");
    	}
    	
    	return ...
    }

 

 

当然也可以在Spring的配置文件中添加如下

	
		
	

 

然后在Struts的Action中取得multipartResolver来用。

 

 

//jsp code
File:

 

 

要注意的是,这个Action在 struts-config.xml 中不能配置ActionForm Bean

参考

http://www.iteye.com/topic/26427

 

你可能感兴趣的:(java,Spring,Struts,Bean,MVC,JSP)