How do we upload files

本文介绍了如何使用Struts2框架进行文件上传操作,包括利用MultipartRequestWrapper和fileUpload拦截器的方法。此外还详细说明了如何处理多个文件上传及配置相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml 代码
  1. How do we upload files   
  2.  Edit Page    Browse Space    Add Page    Add News    
  3. Added by tm_jee, last edited by Ted Husted on Jan 28, 2007  (view change)    
  4. You can obtain the MultipartRequestWrapper from the ServletActionContext or by utilizing the fileUpload interceptor. The fileUpload interceptor is preferred.   
  5.   
  6. Ask the ServletActionContext   
  7. MultipartRequestWrapper multipartRequest = ((MultipartRequestWrapper)ServletActionContext.getRequest())   
  8. The MultipartRequestWrapper provideds access methods such as getFiles, getFile, getContentType, hasErrors, getErrors, and so forth, so that you can process the file uploaded.   
  9.   
  10. Utilize the fileUpload Interceptor   
  11.  _Preferred_   
  12.   
  13. Ensure that {{fileUpload }} Interceptor is included in the Action's stack.    
  14.  The default stack already includes {{fileUpload }}.    
  15. Ensure that the HTML form sets the enctype and specifies on or more file type inputs.    
  16. <form name="myForm" enctype="multipart/form-data">  
  17.      <input type="file" name="myDoc" value="Browse ..." />  
  18.      <input type="submit" />  
  19.   </form>  
  20. Ensure that the Action provides one or more fileUpload mutator methods, with names that correspond to name of the file type input.    
  21. public void setMyDoc(File myDoc)   
  22. public void setMyDocContentType(String contentType)   
  23. public void setMyDocFileName(String filename)   
  24. The Action may also provide the corresponding accessor methods.    
  25. public File getMyDoc()   
  26. public ContentType getMyDocContentType()   
  27. public String getMyDocFileName()   
  28. Handling multiple files   
  29. When multiple files are uploaded by a form, the files are represented by an array.   
  30.   
  31. Given:   
  32.   
  33. <form name="myForm" enctype="multipart/form-data">  
  34.       <input type="file" name="myDoc" value="Browse File A ..." />  
  35.       <input type="file" name="myDoc" value="Browse File B ..." />  
  36.       <input type="file" name="myDoc" value="Browse File C ..." />  
  37.       <input type="submit" />  
  38.    </form>  
  39. The Action class can define file handling methods that accept an array.   
  40.   
  41. public void setMyDoc(File[] myDocs)   
  42. public void setMyDocContentType(String[] contentTypes)   
  43. public void setMyDocFileName(String[] fileNames)   
  44. The uploaded files can be handled by iterating through the appropriate array.   
  45.   
  46. Extra Information   
  47. Property  Default     
  48. struts.multipart.parser  Commons FileUpload     
  49. struts.multipart.saveDir  javax.servlet.context.tempdir as defined by container     
  50. struts.multipart.maxSize  Approximately 2M     
  51.   
  52. @see struts.properties   
  53. @see org.apache.struts2.dispatcher.FilterDispatcher#doFilter(SerlvetRequest, ServletRepsonse, FilterChain)   
  54. @see org.apache.struts2.dispatcher.DispatcherUtil#wrapRequest(HttpServletRequest, SerlvetContext)   
  55. @see org.apache.struts2.dispatcher.multipart.MultipartRequestWrapper   
  56. @see org.apache.struts2.interceptor.FileUploadInterceptor   
  57.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值