现在的位置: 首页 > 综合 > 正文

JAVA上传文件 DiskFileUpload组件

2013年05月22日 ⁄ 综合 ⁄ 共 3544字 ⁄ 字号 评论关闭
commons fileupload 是Apache commons项目的一部分,FileUpload 使你很容易在servlet及web 应用中提供一个鲁棒的、高性能的文件上特性。FileUpload按照RFC 1867 ( "Form-based File Upload in HTML")处理HTTP请求。即,如果HTTP request 以 POST方法提交,并且content type 设置为"multipart/form-data",那么FileUpload可以处理该请求,在web应用中提供文件上载的功能。其使用方法见commons fileupload的相关文档。

在把FileUpload与struts结合(jsp + uploadactiono)使用过程中发现,如果在action mapping配置中不指定formbean,文件上传过程正常。如果指定了formbean,文件上传不正常,取不到文件。以下是几个文件片断:

upload.jsp 

**form action="uploadaction.do?method=uploadByFileUpload" method="post" enctype="multipart/form-data" ** **input type="file" name="uploadfile"** **/form**UploadAction.java public ActionForward uploadByFileUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ...... 
String dir = 
request.getSession().getServletContext().getRealPath( 
"/"); 
DiskFileUpload fu= new DiskFileUpload(); 

fu.setSizeMax( UPLOAD_MAXSIZE); 
fu.setSizeThreshold( MAX_DATA_IN_MEM); 
fu.setRepositoryPath( System.getProperty("java.io.tmpdir")); 

try { 
List fileItem= fu.parseRequest( request); 
Iterator it= fileItem.iterator(); 
while( it.hasNext()){ 
FileItem item= (FileItem)it.next(); 
if( !item.isFormField() && null!= item.getName() && 
0!= item.getName().trim().length()){ 
String clientPath = item.getName(); 
String fileName = new File(clientPath).getName(); 

File destDir = new File( dir); 

File file = new File(destDir, fileName); 
item.write( file); 
map.put( item.getFieldName(), 
dir+ File.separator+ fileName); 


} 
} 
} catch (Exception e) { 
String str= "文件上载异常,错误信息:"+ e.getMessage(); 
System.out.println(str); 
throw new Exception( str, e); 
} 

...... 
} 

struts-config.xml 

name="TestForm" 
type="UploadAction" 
parameter="method" > 

现象:在struts-config.xml文件中,如果指定了formbean——name="TestForm" ,则文件无法正确上传,UploadAction中的fu.parseRequest( request)方法返回值为null;如果去掉了说明formbean的name属性,则文件可以正常上传。

原因:struts的RequestProccessor.process已经包含了处理文件上传的方法。如果在action配置中设置了formbean ,那么在你自己的action处理request之前,struts已经在RequestProccessor.populate方法中处理了request,因此,在自己的action中就取不到上传的文件了。

处理:如果要自己在action中处理文件上传工作,那么就不要在配置文件中配置formbean。

其他选择:如果仍需使用formbean,那么可以使用struts内置的文件上传功能。具体使用方法见struts的相关文档,以及struts的upload例子。以下是几个文件片断:

upload.jsp
基本同上,修改form标签的action属性

UploadAction.java 
public ActionForward uploadByStruts(ActionMapping mapping, 
ActionForm form, 
HttpServletRequest request, 
HttpServletResponse response) 
throws Exception { 
ActionErrors errs= new ActionErrors(); 

if (form != null){ 
DynaActionForm theForm = (DynaActionForm)form; 
FormFile file = (FormFile)theForm.get("uploadfile"); 
try{ 
String fileName= file.getFileName(); 
if ("".equals(fileName)) {return null;} 
InputStream stream = file.getInputStream(); 

String dir = 
request.getSession().getServletContext().getRealPath( 
"/"); 
OutputStream bos = new FileOutputStream(dir+"/"+fileName); 
int bytesRead = 0; 
byte[] buffer = new byte[8192]; 
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { 
bos.write(buffer, 0, bytesRead); 
} 
bos.close(); 
stream.close(); 
}catch (FileNotFoundException fnfe) { 
... 
}catch (IOException ioe) { 
... 
}catch (NullPointerException e){ 
... 
} 

}else{ 
... 
} 

if (!errs.isEmpty()){ 
saveErrors( request, errs); 
} 

return mapping.findForward( "success"); 

} 


struts-config.xml 

**form-bean name="TestForm" type="org.apache.struts.action.DynaActionForm"** 
form-property name="uploadfile" type="org.apache.struts.upload.FormFile" /** 
/form-bean** 

**action path="/uploadaction" 
name="TestForm" **!--指定formbean--** 
type="UploadAction" 
parameter="method" ** 
**forward name="success" path="/success.jsp" /** 
**/action** 

**controller maxFileSize="2M" /** 

注意,使用struts自带的文件上传功能,最带文件尺寸限制用来配置。另为struts对文件上载功能提供了两种处理实现:org.apache.struts.upload.CommonsMultipartRequestHandler 和 org.apache.struts.upload.DiskMultipartRequestHandler。struts默认的是前者,如果要使用后者,需在中配置,配置样例如下。而且,DiskMultipartRequestHandler是使用commons uploadload实现的。

【上篇】
【下篇】

抱歉!评论已关闭.