ITeye博客

How do we upload files

Allen.CD.China · 2007-10-10 16:47 · Struts · Apache · Struts · XML · Access · Servlet · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。