UploadFileAction.java(上传文件Action)

 

  1. package com.blog.action.upload;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7.  
  8. import com.blog.util.Constant;  
  9. import com.blog.util.file.FileUtil;  
  10. import com.opensymphony.xwork2.ActionSupport;  
  11.  
  12. public class UploadFileAction extends ActionSupport {  
  13.       
  14.     /**  
  15.      * <param name="allowTypes">     
  16.     p_w_picpath/bmp,p_w_picpath/png,p_w_picpath/gif,p_w_picpath/jpeg     
  17. </param>   
  18.      */ 
  19.     private static final long serialVersionUID = 1L;  
  20.     /** 上传文件对象 */ 
  21.     private File upload;  
  22.       
  23.     /** 上传文件文件名 */ 
  24.     private String uploadFileName;  
  25.       
  26.     /** 上传文件文件类型 */ 
  27.     private String uploadContentType;  
  28.       
  29.     /** 允许上传文件类型 */ 
  30.     private String allowTypes = "p_w_picpath/bmp,p_w_picpath/png,p_w_picpath/gif,p_w_picpath/jpeg";  
  31.     /**  
  32.      * 复制文件到本地目录  
  33.      *   
  34.      * @param origPath  
  35.      * @param targPath  
  36.      */ 
  37.     private void copy(String savePath, String saveToDBPath){  
  38.           
  39.         System.out.println("开始上传单个文件--------------------");  
  40.         System.out.println(savePath);  
  41.         System.out.println("============" + getUploadFileName());  
  42.         System.out.println("============" + getUploadContentType());  
  43.         System.out.println("============" + getUpload());  
  44.           
  45.         //以服务器的文件保存地址和原文件名建立上传文件输出流  
  46.         try {  
  47.             FileOutputStream fos = new FileOutputStream(savePath + "/" + this.getUploadFileName());  
  48.               
  49.             FileInputStream fis = new FileInputStream(getUpload());  
  50.               
  51.             byte[] buffer = new byte[1024];  
  52.               
  53.             int len = 0 ;  
  54.               
  55.             while((len = fis.read(buffer)) > 0){  
  56.                 fos.write(buffer, 0, len);  
  57.             }  
  58.               
  59.             //关闭流  
  60.             fis.close();  
  61.             fos.close();  
  62.         } catch (FileNotFoundException e) {  
  63.             // TODO Auto-generated catch block  
  64.             e.printStackTrace();  
  65.         } catch(Exception exception){  
  66.             exception.printStackTrace();  
  67.         }  
  68.           
  69.     }  
  70.       
  71.     /**  
  72.      * 上传文件  
  73.      */ 
  74.     public String upload(){  
  75.           
  76.         FileUtil fileUtil = FileUtil.getFileUtil();  
  77.           
  78.         // 文件保存路径  
  79.         String savePathRoot = Constant.UPLOAD_FILE_SAVE_ROOT + "bus/" + fileUtil.getDateFolderName();  
  80.         // 数据库保存路径  
  81.         String saveToDBRoot = "bus/" + fileUtil.getDateFolderName() + "/" + this.getUploadFileName();  
  82.         //创建该文件目录  
  83.         fileUtil.createFolder(savePathRoot);  
  84.         //开始复制文件  
  85.         this.copy(savePathRoot, saveToDBRoot);  
  86.           
  87.         return SUCCESS;  
  88.     }  
  89.  
  90.     public String execute(){  
  91.           
  92.         return this.upload();  
  93.           
  94.     }  
  95.       
  96.       
  97.     public File getUpload() {  
  98.         return upload;  
  99.     }  
  100.  
  101.     public void setUpload(File upload) {  
  102.         this.upload = upload;  
  103.     }  
  104.  
  105.     public String getUploadFileName() {  
  106.         return uploadFileName;  
  107.     }  
  108.  
  109.     public void setUploadFileName(String uploadFileName) {  
  110.         this.uploadFileName = uploadFileName;  
  111.     }  
  112.  
  113.     public String getUploadContentType() {  
  114.         return uploadContentType;  
  115.     }  
  116.  
  117.     public void setUploadContentType(String uploadContentType) {  
  118.         this.uploadContentType = uploadContentType;  
  119.     }  
  120.  
  121.     public String getAllowTypes() {  
  122.         return allowTypes;  
  123.     }  
  124.  
  125.     public void setAllowTypes(String allowTypes) {  
  126.         this.allowTypes = allowTypes;  
  127.     }  
  128.       
  129. }  

辅助类:FileUtil

 

  1. package com.blog.util.file;  
  2.  
  3. import java.io.File;  
  4. import java.util.Date;  
  5.  
  6. import com.blog.util.date.DateUtil;  
  7.  
  8. public class FileUtil {  
  9.       
  10.     private static FileUtil fileUtil = new FileUtil();  
  11.       
  12.     private FileUtil(){  
  13.           
  14.     }  
  15.       
  16.     /**  
  17.      * 创建指定目录文件夹  
  18.      *   
  19.      * @param dst  
  20.      * @return boolean  
  21.      */ 
  22.     public boolean createFolder(String dst){  
  23.           
  24.         boolean result = false;  
  25.           
  26.         File file = new File(dst);  
  27.           
  28.         file.delete();//删除文件夹  
  29.           
  30.         if(!file.exists()){  
  31.             result = file.mkdirs();  
  32.         }  
  33.           
  34.           
  35.         return result;  
  36.     }  
  37.       
  38.     /**  
  39.      * 获取日期名文件夹字符串  
  40.      *   
  41.      * @return String  
  42.      */ 
  43.     public String getDateFolderName(){  
  44.           
  45.         DateUtil  dateUtil = DateUtil.getDateUtil();  
  46.         Date date = dateUtil.getDate();  
  47.         StringBuffer sBuffer = new StringBuffer();  
  48.           
  49.         sBuffer.append(String.valueOf(date.getYear()+1900));  
  50.         sBuffer.append(String.valueOf(date.getMonth() + 1));  
  51.         sBuffer.append(String.valueOf(date.getDate()));  
  52.         sBuffer.append(String.valueOf(date.getHours()));  
  53.         sBuffer.append(String.valueOf(date.getMinutes()));  
  54.         sBuffer.append(String.valueOf(date.getSeconds()));  
  55.           
  56.         return sBuffer.toString();  
  57.           
  58.     }  
  59.  
  60.     /**  
  61.      * 获取文件工具类对象  
  62.      *   
  63.      * @return FileUtil  
  64.      */ 
  65.     public static FileUtil getFileUtil() {  
  66.         return fileUtil;  
  67.     }  
  68.       
  69. }  

辅助类:Constant

 

  1. package com.blog.util;  
  2.  
  3. public interface Constant {  
  4.       
  5.     /** 上传文件根目录 */ 
  6.     public static final String UPLOAD_FILE_SAVE_ROOT = "F:/upload/";  
  7. }