---
title: "How do we upload files"
author: "Allen.CD.China"
date: "2007-10-10T16:47:08Z"
tags: ["Access", "Apache", "Servlet", "Struts", "XML"]
slug: "how-do-we-upload-files"
draft: false
lastmod: "2007-10-10T16:47:08Z"
id: 130822
url: "https://allenj2ee.iteye.com/blog/130822"
---

xml 代码

01. How do we upload files
02. Edit Page    Browse Space    Add Page    Add News
03. Added by tm\_jee, last edited by Ted Husted on Jan 28, 2007  (view change)
04. You can obtain the MultipartRequestWrapper from the ServletActionContext or by utilizing the fileUpload interceptor. The fileUpload interceptor is preferred.
05. Ask the ServletActionContext
06. MultipartRequestWrapper multipartRequest = ((MultipartRequestWrapper)ServletActionContext.getRequest())
07. The MultipartRequestWrapper provideds access methods such as getFiles, getFile, getContentType, hasErrors, getErrors, and so forth, so that you can process the file uploaded.
08. Utilize the fileUpload Interceptor
09. \_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. &lt;form name="myForm" enctype="multipart/form-data"&gt;
14. &lt;input type="file" name="myDoc" value="Browse ..." /&gt;
15. &lt;input type="submit" /&gt;
16. &lt;/form&gt;
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. &lt;form name="myForm" enctype="multipart/form-data"&gt;
29. &lt;input type="file" name="myDoc" value="Browse File A ..." /&gt;
30. &lt;input type="file" name="myDoc" value="Browse File B ..." /&gt;
31. &lt;input type="file" name="myDoc" value="Browse File C ..." /&gt;
32. &lt;input type="submit" /&gt;
33. &lt;/form&gt;
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