最近項目中使用到文件上傳的例子,用到struts中的文件上傳及ftp簡單總結下:
1.struts文件上傳
2.ftp服務器搭建
3.struts上傳文件到ftp組件
1.struts文件
struts文件上傳相對比較簡單,由于struts對文件上傳進行了封裝,上篇文章中說到的struts中的文件上傳攔截器進行的處理,具體邏輯代碼如下:
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 ValidationAware) {20 validation = (ValidationAware) action;21 }22 23 MultiPartRequestWrapper 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===核心處理代碼邏輯34 //大體邏輯35 //循環遍歷前臺input標簽定義的name列表,每個name對應一個文件列表,遍歷文件列表獲取文件類型及文件內容36 Enumeration fileParameterNames = multiWrapper.getFileParameterNames();37 while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {38 // get the value of this input tag獲取前臺定義的name屬性39 String inputName = (String) fileParameterNames.nextElement();40 41 // get the content type==獲取文件類型42 String[] contentType = multiWrapper.getContentTypes(inputName);43 44 if (isNonEmpty(contentType)) {45 // get the name of the file from the input tag==獲取文件名46 String[] fileName = multiWrapper.getFileNames(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++) {59 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 //文件列表69 params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));70 //文件類型名稱列表71 params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]));72 //文件名稱列表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 }
通過研究上面的代碼可以明白文件上傳的使用方法,比如下面頁面:
前臺頁面代碼如下
1 <div> 2 <label>附件上傳1</label> 3 <input name="file" type="file"> 4 <input name="file" type="file"> 5 </div> 6 <div> 7 <label>附件上傳2</label> 8 <input name="test" type="file"> 9 <input name="test" type="file">10 </div>
對于這樣的前臺定義,后臺action中應該進行如下想關屬性的配置
//對應前臺頁面中的name=“file”的一組定義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 //對應前臺name為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 }
新聞熱點
疑難解答