文件上傳之一文件隨表單其他數據一起上傳

  將文件放在表單中與其他數據一起提交,這是最簡單的文件上傳實現。在做測試或者簡單實驗時可以使用這個方法來實現文件上傳。

  我把demo放在附件了,需要的可以下載附件,把它導進eclipse(Eclipse Java EE IDE for Web Developers),還缺少jar包,可以將struts下的struts2-blank樣例裏的jar拷貝過來。

 表單

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <!DOCTYPE> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. <title>File Upload</title> 
  8. </head> 
  9. <body> 
  10.     <div> 
  11.         <div> 
  12.             <h3>表單提交的方式上傳文件</h3> 
  13.         </div> 
  14.         <div> 
  15.             <form action="noramlFileLoadAction" enctype="multipart/form-data" 
  16.                 method="post"> 
  17.                 <div> 
  18.                     <input type="file" name="file" id="file_upload" /> 
  19.                 </div> 
  20.                 <div> 
  21.                     <input type="submit" value="提交" /> 
  22.                 </div> 
  23.             </form> 
  24.         </div> 
  25.         <div> 
  26.             <hr /> 
  27.         </div> 
  28.     </div> 
  29. </body> 
  30. </html> 

提交後跳轉的頁面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib prefix="s" uri="/struts-tags"%> 
  4. <!DOCTYPE> 
  5. <html> 
  6. <head> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  8. <title>File Upload Result</title> 
  9. </head> 
  10. <body> 
  11.     <div> 
  12.         <
  13.             String isSuccess = request.getParameter("isSuccess"); 
  14.             if ("1".equals(isSuccess)) { 
  15.                 out.print("上傳成功"); 
  16.             } else if ("0".equals(isSuccess)) { 
  17.                 out.print("上傳失敗"); 
  18.             } else { 
  19.                 out.print("上傳結果未知"); 
  20.             } 
  21.             %> 
  22.     </div> 
  23. </body> 
  24. </html> 

Struts2配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  9.     <constant name="struts.ui.theme" value="simple" />  
  10.     <constant name="struts.multipart.maxSize" value="100" />  
  11.     <constant name="struts.multipart.saveDir " value="c:/tmp" />  
  12.   
  13.     <package name="fileUpload" extends="struts-default" namespace="/">  
  14.         <action name="noramlFileLoadAction" class="com.study.action.FileUploadAction"  
  15.             method="execute">  
  16.             <param name="filePath">/filetemp</param>  
  17.             <result name="success">/NomalFileUploadResult.jsp?isSuccess=1</result>  
  18.             <result name="input">/NomalFileUploadResult.jsp?isSuccess=0</result>  
  19.         </action>  
  20.     </package>  
  21.   
  22. </struts>  

FileCopyUtil(文件複製的工具類)

  1. package com.study.util; 
  2.  
  3. import java.io.BufferedInputStream; 
  4. import java.io.BufferedOutputStream; 
  5. import java.io.File; 
  6. import java.io.FileInputStream; 
  7. import java.io.FileOutputStream; 
  8. import java.io.InputStream; 
  9. import java.io.OutputStream; 
  10.  
  11. /** 
  12.  * Filecopy is used to copy file. 
  13.  * @author zhbhun 
  14.  * 
  15.  */ 
  16. public class FileCopyUtil { 
  17.  
  18.     public static final int BUFFER_SIZE = 16 * 1024; 
  19.  
  20.     /** 
  21.      * copy src to dst. 
  22.      * @param src  
  23.      * @param dst 
  24.      * @throws Exception 
  25.      */ 
  26.     public static void copy(File src, File dst) throws Exception { 
  27.         try { 
  28.             InputStream in = null
  29.             OutputStream out = null
  30.             try { 
  31.                 in = new BufferedInputStream(new FileInputStream(src), 
  32.                         BUFFER_SIZE); 
  33.                 out = new BufferedOutputStream(new FileOutputStream(dst), 
  34.                         BUFFER_SIZE); 
  35.                 byte[] buffer = new byte[BUFFER_SIZE]; 
  36.                 while (in.read(buffer) > 0) { 
  37.                     out.write(buffer); 
  38.                 } 
  39.             } finally { 
  40.                 if (null != in) { 
  41.                     in.close(); 
  42.                 } 
  43.                 if (null != out) { 
  44.                     out.close(); 
  45.                 } 
  46.             } 
  47.         } catch (Exception e) { 
  48.             throw new Exception("File copy error.", e); 
  49.         } 
  50.     } 

Action

  1. package com.study.action;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.apache.struts2.ServletActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.study.util.FileCopyUtil;  
  8.   
  9. public class FileUploadAction extends ActionSupport {  
  10.   
  11.     protected File file;  
  12.     protected String fileFileName;  
  13.     protected String fileContentType;  
  14.     protected String filePath;  
  15.   
  16.     protected void restoreFile() throws Exception {  
  17.         if (this.file != null) {  
  18.             File temp = new File(ServletActionContext.getServletContext()  
  19.                     .getRealPath(this.filePath) + "/" + this.fileFileName);  
  20.             FileCopyUtil.copy(this.file, temp);  
  21.         }  
  22.     }  
  23.   
  24.     @Override  
  25.     public String execute() throws Exception {  
  26.         this.restoreFile();  
  27.         return "success";  
  28.     }  
  29.   
  30.     public File getFile() {  
  31.         return file;  
  32.     }  
  33.   
  34.     public void setFile(File file) {  
  35.         this.file = file;  
  36.     }  
  37.   
  38.     public String getFileFileName() {  
  39.         return fileFileName;  
  40.     }  
  41.   
  42.     public void setFileFileName(String fileFileName) {  
  43.         this.fileFileName = fileFileName;  
  44.     }  
  45.   
  46.     public String getFileContentType() {  
  47.         return fileContentType;  
  48.     }  
  49.   
  50.     public void setFileContentType(String fileContentType) {  
  51.         this.fileContentType = fileContentType;  
  52.     }  
  53.   
  54.     public String getFilePath() {  
  55.         return filePath;  
  56.     }  
  57.   
  58.     public void setFilePath(String filePath) {  
  59.         this.filePath = filePath;  
  60.     }  
  61.   
  62. }  

備註:本文不涉及struts2文件上傳的詳細配置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章