ServletFileUpload.parseRequest()得不到请求内容

public void upload(HttpServletRequest request, HttpServletResponse response){
		//创建解析工厂
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setRepository(new File(this.getServletContext().getRealPath("/WEB-INF/temp")));//设置文件缓存目录
		//创建解析器
		ServletFileUpload load = new ServletFileUpload(factory);
		load.setHeaderEncoding("UTF-8");
		//解析request得到封装FileItem的list
		try{List list = load.parseRequest(request);//list.size() is 0
                       for(int i=0; i<list.size(); i++) {
				FileItem item = (FileItem)list.get(i);
				if(!item.isFormField()){
					String name = item.getName();
					if(!name.trim().equals("")){
						name = name.substring(name.lastIndexOf("\\")+1);
						//拿到inoutStream流
						InputStream inputStream = item.getInputStream();
						String path =this.getServletContext().getRealPath("/WEB-INF/upload");
						
						File file = new File(path);
						if(!file.exists())
							file.mkdirs();
						//定义输出流
						OutputStream outputStream = new FileOutputStream(path+"\\"+name);
						
						int len = 0;
						byte data[] = new byte[1024];
						while((len=inputStream.read(data))!=-1)
							outputStream.write(data, 0, len);
						inputStream.close();
						outputStream.flush();
						outputStream.close();
						item.delete();//删除临时文件
					}
				}
			}
			request.setAttribute("message","上传成功");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("message","上传失败");
		}finally{
			try {
				request.getRequestDispatcher("/message.jsp").forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}...

 Cauz:struts的拦截器机制RequestProccessor自己处理了文件上传, 如果是struts1,去掉formbean中的file对应属性, 如果是struts2,把

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

里面的/*改成/*.action然后用普通的servlet代替action做上传处理,webwork同理

<filter>
	<filter-name>webwork</filter-name>
	<filter-class>
		com.opensymphony.webwork.dispatcher.FilterDispatcher
	</filter-class>
</filter>
<filter-mapping>
		<filter-name>webwork</filter-name>
		<url-pattern>*.action</url-pattern>
</filter-mapping>

 

 

你可能感兴趣的:(Web,jsp,struts,servlet,Webwork)