首页 > 代码库 > 开发日志:struts2使用commons.fileupload上传附件,并解决upload.parseRequest(request)为空的问题

开发日志:struts2使用commons.fileupload上传附件,并解决upload.parseRequest(request)为空的问题

要做一个phongap开发的App上传文件到服务器的Action,打算使用commons.fileupload的方式

接口jsp页面

<form  action="uploadAction.action" method="post" enctype="multipart/form-data">
			<table>
				<tr><td>上传附件:uploadAction.action</td></tr>
				<tr><td><input name="file" type="file" size="20" ></td><td>上传图片</td></tr>
				<tr><td colspan="2" align="center"><input type="submit" value=http://www.mamicode.com/"上传"/>>

保存附件的Action方法

/**
	 * 初始化目录(如果目录不存在,新建)
	 * @param uploadPath 保存路径
	 * @param tempPath 临时文件路径
	 * @throws ServletException
	 */
	private void initDir(String uploadPath,String tempPath) throws ServletException{
		File uploadFile = new File(uploadPath);
		if (!uploadFile.exists()) {
			uploadFile.mkdirs();
		}
		File tempPathFile = new File(tempPath);
		if (!tempPathFile.exists()) {
			tempPathFile.mkdirs();
		}
	}
	
	public String uploadAction()throws Exception{
		try {
			Date date = new Date();
			//上传文件
			request.setCharacterEncoding("UTF-8");
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
			String basePath = ServletActionContext.getRequest().getRealPath("/");//获取根目录
			//设置下载目录
			String uploadPath = basePath + "/upload";
			//设置临时目录
			String tempPath = uploadPath +"/temp";
			//uploadPath = uploadPath +"/"+ date.getTime();//添加时间目录防止重复
			this.initDir(uploadPath, tempPath);
			File tempPathFile = new File(tempPath);
	        factory.setRepository(tempPathFile);// 设置缓冲区目录
			ServletFileUpload upload = new ServletFileUpload(factory);
			//upload.setSizeMax(34194304); 设置最大文件尺寸,4MB
			List items = upload.parseRequest(request);//得到所有的文件
			Iterator itr = items.iterator();
			
			
			while(itr.hasNext()){
				FileItem fileItem = (FileItem) itr.next();
				if(fileItem.isFormField()){
					System.out.println("表单参数名:"+fileItem.getFieldName()+",表单参数值:"+fileItem.getString("utf-8"));
				}else{
					if(fileItem.getName()!=null && !"".equals(fileItem.getName())){
						System.out.println("上传文件的大小:"+fileItem.getSize());
						System.out.println("上传文件的类型:"+fileItem.getContentType());
						System.out.println("上传文件的名称:"+fileItem.getName());
						//保存文件
						File saveFile = new File(uploadPath,fileItem.getName());
						fileItem.write(saveFile);
						imgUrl = uploadPath+"/"+fileItem.getName();
						System.out.println("上传成功");
					}else{
						System.out.println("没有上传文件");
					}
				}
				
			}
			
			
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

调用Action的时候发现
List items = upload.parseRequest(request);

items的值一直为空,网上找了下原因:参考http://blog.csdn.net/qq964166471/article/details/21385041

原因是struts2把原始的原来S2为简化上传功能,把所有的enctype="multipart/form-data"表单做了wrapper最后把HttpServletResquest封装成 org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper


我采用的解决步骤

新建MyRequestParser类,重写parse方法,里面内容为空

public class MyRequestParser extends JakartaMultiPartRequest{

	@Override
	public void parse(HttpServletRequest servletRequest, String saveDir) throws IOException {
		
	}
	
}

然后在struts下增加下面内容

<struts>
	
	<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" 
		  name="myRequestParser" class="com.MyRequestParser" 
		  scope="default" optional="true"/>
	<constant name="struts.multipart.handler" value=http://www.mamicode.com/"myRequestParser" />  


重启tomcat后上传成功


开发日志:struts2使用commons.fileupload上传附件,并解决upload.parseRequest(request)为空的问题