status2.3.4和struts2.3.15.1 Fileupload上传文件简单分析

    struts2漏洞爆发,近日修补漏洞,struts2.3.4.1升级到struts2.3.15.1,发现以前用uploadFile能正常上传文件显示进度条现在不能正常工作了,List items = upload.parseRequest(request),items为空,得不到HttpServletRequest值了。
    研究了一天,也在网上转了一天,终于得到灵感,查看了一下struts2.3.15.1的struts-default.xml配置文件,发现<constant name="struts.multipart.handler" value="jakarta" />已经变成了parser,在原来的项目中把struts.multipart.handler换成了struts.multipart.parser,一切OK,正常了。
 
配置完成了,就可以直接利用uploadFile中的监听器来完成文件上传进度的判断了和上传文件了。
 
 
这是struts2.3.15.1的默认配置
<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default"/>
    <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="jakarta" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" />
    <constant name="struts.multipart.parser" value="jakarta" />
 
 
Struts2 中用Fileupload上传文件得不到HttpServletRequest值的解决办法
 
在status2 .3.4.1的配置方式

在status2 .3.4.1的配置文件里加上
<bean   type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest" 
name= "myRequestParser"   class= "com.icf.common.base.actions.RequestParseWrapper" 
scope= "default"   optional= "true "   /> 
<constant   name= "struts.multipart.handler"   value= "myRequestParser"   />


class
public class RequestParseWrapper  extends JakartaMultiPartRequest   {

publicvoid parse(HttpServletRequest   servletRequest,   String   saveDir)throws   IOException{   
    } 
}


这样就可以得到request里的item的值了。

struts2.3.15.1的配置方式

在struts2.3.15.1后的配置里,struts2的struts-default.xml 里把 struts.multipart.handler 改成了 struts.multipart.parser 

在struts配置文件中
<bean   type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest" 
name= "myRequestParser"   class= "com.icf.common.base.actions.RequestParseWrapper" 
scope= "default"   optional= "true "   /> 
<constant   name= " struts.multipart.parser"   value= "myRequestParser"   />
class
public class RequestParseWrapper  extends JakartaMultiPartRequest   {

publicvoid parse(HttpServletRequest   servletRequest,   String   saveDir)throws   IOException{   
    } 
}

下面的片断Class是利用spring mvc做的一个上传文件Controller中的一个方法
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
	public void  uploadFile(HttpServletRequest req,HttpServletResponse resp)  {
		final HttpSession session = req.getSession();
		String path = session.getServletContext().getRealPath("/");
		File file = new File(path + "/upload");
		if(!file.exists()) file.mkdirs();
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if (!isMultipart) return;
		
			  // Create a factory for disk-based file items 
			 FileItemFactory factory = new DiskFileItemFactory();  
			// Create a new file upload handler  
		       ServletFileUpload upload = new ServletFileUpload(factory);  
		       //上传文件大小最大不能超过60M
		       upload.setFileSizeMax(60000000);
		       upload.setHeaderEncoding("UTF-8");
		       
		       upload.setProgressListener(new ProgressListener() {
				@Override
				public void update(long pBytesRead, long pContentLength, int pItems) {
					ProcessInfo pri = new ProcessInfo();
					pri.itemNum = pItems;
					pri.readSize = pBytesRead;
					pri.totalSize = pContentLength;
				    pri.show =((float)pBytesRead/1000)+"/"+((float)pContentLength/1000)+" Kbyte";  
				    pri.rate = Math.round((float)pBytesRead/(float)pContentLength*100);
				    session.setAttribute("proInfo", pri);
				    try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				    System.out.println("已经上传 " +pri.rate);
				    
				}
			} );
		       
		       List<FileItem> items;
			
	try {
		items = upload.parseRequest(req);

      System.out.println("===========================================");		    
      for(FileItem item: items) {
		  System.out.println("ContentType "+ item.getContentType()); 
		  System.out.println(" 字段名 " + item.getFieldName());
		  System.out.println("文件名   "+item.getName());
		  System.out.println("文件大小 " + item.getSize());
		 
		  item.write(new File(file,item.getName()));
      }
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
	}
}
 

你可能感兴趣的:(java,struts2,uploadfile)