struts2單文件上傳

1.配置web.xml(上一篇有)

2.配置struts.xml

Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.     <struts>  
  6.         <constant name="struts.i18n.encoding" value="UTF-8"></constant>  
  7.         <constant name="struts.configuration.xml.reload" value="true" />  
  8.         <constant name="struts.devMode" value="false" />  
  9.         <include file="com/struts/config/file.xml"></include>  
  10.     </struts>  

 3. FileAction  類

Java代碼
  1. package com.file.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7.   
  8. import com.opensymphony.xwork2.ActionSupport;  
  9.   
  10. public class FileAction extends ActionSupport{  
  11.   
  12.     private File uploadFile;  
  13.     private String uploadFileContentType;  
  14.     private String uploadFileFileName;  
  15.       
  16.     public String file(){  
  17.         System.out.println("11111111111111111111");  
  18.         String realPath = "e:\\現計/piture";  
  19.         File file  = new File(realPath);  
  20.         if(!file.exists()){  
  21.             file.mkdirs();  
  22.         }  
  23.         try {  
  24.             FileUtils.copyFile(uploadFile, new File(file, uploadFileFileName));  
  25.         } catch (IOException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();   
  28.             return "input";  
  29.         }  
  30.         return "success";  
  31.     }  
  32.   
  33.     /**********get 和 set方法************/  
  34.     public File getUploadFile() {  
  35.         return uploadFile;  
  36.     }  
  37.   
  38.     public void setUploadFile(File uploadFile) {  
  39.         this.uploadFile = uploadFile;  
  40.     }  
  41.   
  42.     public String getUploadFileContentType() {  
  43.         return uploadFileContentType;  
  44.     }  
  45.   
  46.     public void setUploadFileContentType(String uploadFileContentType) {  
  47.         this.uploadFileContentType = uploadFileContentType;  
  48.     }  
  49.   
  50.     public String getUploadFileFileName() {  
  51.         return uploadFileFileName;  
  52.     }  
  53.   
  54.     public void setUploadFileFileName(String uploadFileFileName) {  
  55.         this.uploadFileFileName = uploadFileFileName;  
  56.     }  
  57. }  

 4.配置 file.xml

Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.     <struts>  
  6.         <constant name="struts-multipart.maxSize" value="16777216"></constant>  
  7.   
  8.         <package name="testo" extends="struts-default">  
  9.             <action name="f_*" class="com.file.action.FileAction" method="{1}">  
  10.                 <result   name="success">/success.jsp</result>  
  11.                 <result name="input">/default.jsp</result>  
  12.             </action>  
  13.         </package>  
  14.     </struts>  

 5.jsp頁面訪問

Java代碼
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>文件上傳</title>  
  8. </head>  
  9. <body>  
  10. 文件上傳  
  11. <form action="f_file.action" method="post" enctype="multipart/form-data">  
  12.     <input type="file" name="uploadFile">  
  13.     <input  type="submit" value="上傳">  
  14. </form>  
  15. </body>  
  16. </html> 
發佈了53 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章