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();
	}
}
相關文章
相關標籤/搜索