Struts file upload and struts2 File Upload
Examples of using File Upload in recent projects, using File Upload in struts and a simple summary of ftp:
1. Upload struts files
2. Build an ftp server
3. upload files to the ftp component using struts
1. struts File
Struts File Upload is relatively simple. Because struts encapsulates file upload, the file upload interceptor in struts mentioned in the previous article is processed. The specific logic code is as follows:
1 public String intercept (ActionInvocation invocation) throws Exception {2 ActionContext ac = invocation. getInvocationContext (); 3 4 HttpServletRequest request = (HttpServletRequest) ac. get (ServletActionContext. HTTP_REQUEST); 5 6 if (! (Request instanceof MultiPartRequestWrapper) {7 if (LOG. isDebugEnabled () {8 ActionProxy proxy = invocation. getProxy (); 9 LOG. debug (getTextMessage ("struts. messages. bypass. request ", new String [] {proxy. getNamespace (), proxy. getActionName ()}); 10} 11 12 return invocation. invoke (); 13} 14 15 ValidationAware validation = null; 16 17 Object action = invocation. getAction (); 18 19 if (action instanceof Val IdationAware) {20 validation = (ValidationAware) action; 21} 22 23 multipartreququestwrapper multiWrapper = (MultiPartRequestWrapper) request; 24 25 if (multiWrapper. hasErrors () {26 for (String error: multiWrapper. getErrors () {27 if (validation! = Null) {28 validation. addActionError (error ); 29} 30} 31} 32 33 // bind allowed Files === core processing code logic 34 // general logic 35 // cyclically traverse the name list defined by the front-end input tag, each name corresponds to a file list and traverses the file list to obtain the file type and content 36 Enumeration fileParameterNames = multiWrapper. getFileParameterNames (); 37 while (fileParameterNames! = Null & fileParameterNames. hasMoreElements () {38 // get the value of this input tag get the name attribute defined at the front end 39 String inputName = (String) fileParameterNames. nextElement (); 40 41 // get the content type = get file type 42 String [] contentType = multiWrapper. getContentTypes (inputName); 43 44 if (isNonEmpty (contentType) {45 // get the name of the file from the input tag = get the file name 46 String [] fileName = multiWrapper. getFile Names (inputName); 47 48 if (isNonEmpty (fileName) {49 // get a File object for the uploaded File50 File [] files = multiWrapper. getFiles (inputName); 51 if (files! = Null & files. length> 0) {52 List <File> acceptedFiles = new ArrayList <File> (files. length); 53 List <String> acceptedContentTypes = new ArrayList <String> (files. length); 54 List <String> acceptedFileNames = new ArrayList <String> (files. length); 55 String contentTypeName = inputName + "ContentType"; 56 String fileNameName = inputName + "FileName"; 57 58 for (int index = 0; index <files. length; index ++) {5 9 if (acceptFile (action, files [index], fileName [index], contentType [index], inputName, validation) {60 acceptedFiles. add (files [index]); 61 acceptedContentTypes. add (contentType [index]); 62 acceptedFileNames. add (fileName [index]); 63} 64} 65 66 if (! AcceptedFiles. isEmpty () {67 Map <String, Object> params = ac. getParameters (); 68 // file list 69 params. put (inputName, acceptedFiles. toArray (new File [acceptedFiles. size ()]); 70 // file type name list 71 params. put (contentTypeName, acceptedContentTypes. toArray (new String [acceptedContentTypes. size ()]); 72 // file name list 73 params. put (fileNameName, acceptedFileNames. toArray (new String [acceptedFileNames. size ()]); 74} 75} 76} else {77 if (LOG. isWarnEnabled () {78 LOG. warn (getTextMessage (action, "struts. messages. invalid. file ", new String [] {inputName}); 79} 80} 81} else {82 if (LOG. isWarnEnabled () {83 LOG. warn (getTextMessage (action, "struts. messages. invalid. content. type ", new String [] {inputName}); 84} 85} 86} 87 88 // invoke action89 return invocation. invoke (); 90}
By studying the code above, you can understand how to use file upload, such as the following page:
The front-end Page code is as follows:
1 <div> 2 <label> attachment upload 1 </label> 3 <input name = "file" type = "file"> 4 <input name = "file" type =" file "> 5 </div> 6 <div> 7 <label> attachment upload 2 </label> 8 <input name =" test "type =" file "> 9 <input name = "test" type = "file"> 10 </div>
For such foreground definition, the following attributes should be configured in the background action:
// A set of definitions corresponding to name = "file" on the front-end page: private List <File> file; private List <String> fileContentType; private List <String> fileFileName; public List <File> getFile () {return file;} public void setFile (List <File> file) {this. file = file;} public List <String> getFileContentType () {return fileContentType;} public void setFileContentType (List <String> fileContentType) {this. fileContentType = fileContentType;} public List <String> getFileFileName () {return fileFileName;} public void setFileFileName (List <String> fileFileName) {this. fileFileName = fileFileName ;}
1 // List of attribute files whose frontend name is test 2 private List <File> test; 3 private List <String> testContentType; 4 private List <String> testFileName; 5 public List <File> getTest () {6 return test; 7} 8 9 public void setTest (List <File> test) {10 this. test = test; 11} 12 13 public List <String> getTestContentType () {14 return testContentType; 15} 16 17 public void setTestContentType (List <String> testContentType) {18 this. testContentType = testContentType; 19} 20 21 public List <String> getTestFileName () {22 return testFileName; 23} 24 25 public void setTestFileName (List <String> testFileName) {26 this. testFileName = testFileName; 27}