struts2上传下载工具类(创建文件夹及删除文件)

  1 package util.zl;

  2 

  3 import java.io.File;

  4 import java.io.FileNotFoundException;

  5 import java.io.IOException;

  6 import java.io.InputStream;

  7 

  8 import org.apache.commons.io.FileUtils;

  9 import org.apache.struts2.ServletActionContext;

 10 

 11 import com.opensymphony.xwork2.ActionSupport;

 12 

 13 

 14 public class file extends ActionSupport{

 15 

 16 

 17   /**

 18    * @author 文件下载

 19       * @getDownloadFile

 20       * 此方法对应的是struts.xml文件中的:

 21       * <param name="inputName">downloadFile</param>

 22       * 返回下载文件的流,可以参看struts2的源码

 23    * @param path  文件保存-根目录名

 24    * @param userName 文件保存-用户目录名

 25    * @param fileName 

 26    * @return

 27    * @throws FileNotFoundException

 28    */

 29   public static InputStream getDown(String path,String fileName) throws FileNotFoundException

 30   {

 31 

 32    String pathstr=path+"/"+fileName;

 33 

 34    return new java.io.FileInputStream(pathstr);

 35   } 

 36   

 37   /**

 38    * @author   推荐使用方法

 39    * @param path 文件保存目录(包括文件名)

 40    * @return

 41    * @throws FileNotFoundException

 42    */

 43   public static InputStream getDown(String path) 

 44   { 

 45    String pathstr=path;

 46    InputStream input = null;

 47    try {

 48     input = new java.io.FileInputStream(pathstr);

 49    } catch (FileNotFoundException e) {

 50     // TODO Auto-generated catch block

 51     e.printStackTrace();

 52    }

 53    return input;

 54   } 

 55  

 56   /**

 57    * @author  文件夹的建立

 58    * @param  rootPath

 59    * @param  userName

 60    * @param  field_name

 61    * @return

 62    * @throws  Exception

 63    */

 64   public static String createFiled(String rootPath,String userName, String field_name) throws Exception {

 65 

 66    String path=rootPath+userName+"\\"+field_name;

 67    System.out.println("文件夹路径:"+path);

 68     

 69     File Filed = null;

 70     try {

 71      Filed = new File(path);

 72     } catch (Exception e) {

 73 

 74      e.printStackTrace();

 75     }

 76 

 77     if(!Filed.exists()){

 78      

 79      Filed.mkdir();//建立新文件(destFile)的父目录

 80     }

 81    return path;

 82   }

 83   

 84   

 85   /**删除文件*/

 86   public static String dropFiled(String rootPath) throws Exception {

 87 

 88    String path=rootPath;    

 89     File Filed = null;

 90     try {

 91      Filed = new File(path);

 92     } catch (Exception e) {

 93 

 94      e.printStackTrace();

 95     }

 96 

 97     if(Filed.exists()){

 98      

 99      Filed.delete();//删除文件(destFile)的父目录

100     }

101    return path;

102   }

103   

104   

105   

106    /**

107     * @author  推荐使用-自定义目录( d:\\admin\\20101105\\*.*) 2010.11.05

108     * @param  file  ①原始文件

109     * @param  fileName ②文件名

110     * @param  rootPath 文件保存-根目录

111     * @param  userName 文件保存-用户目录名

112     * @param  fieldName 文件保存-文件夹名

113     * @return path  文件保存全路径(包括文件名)

114     * @throws  Exception 

115     */

116    public static String saveFile( File file,String fileName,String rootPath,String userName, String fieldName) {

117   

118     System.out.println("正在保存文件:filename:"+fileName);

119     

120     // String path = ServletActionContext.getServletContext().getRealPath("d:/gggg/"+username+"/"+field_name);

121      

122     String path=rootPath+userName+"/"+fieldName+"/"; 

123     String filePath=path+fileName;

124     

125     if(file!=null){

126      

127      File newFile=new File(new File(path),fileName);

128      

129      if(!newFile.getParentFile().exists()){   

130       

131       newFile.getParentFile().mkdir();   

132      }

133      try {

134       FileUtils.copyFile(file, newFile); //Struts2 Api

135       System.out.println("文件保存路径"+filePath);

136      } catch (IOException e) {

137       e.printStackTrace();

138       System.out.println("文件保存出错");

139      }    

140      

141     }

142     else 

143      System.out.println("没有文件可以保存!");

144     

145     return filePath;

146    }

147    

148    /**

149     * @author  项目所在目录( /admin/20101105/*.*) 2010.11.05

150     * @param  file  ①原始文件

151     * @param  fileName ②文件名

152     * @param  rootPath 文件保存-根目录

153     * @param  userName 文件保存-用户目录名

154     * @param  fieldName 文件保存-文件夹名

155     * @return path  文件保存的路径

156     * @throws  Exception

157     */

158    public static String saveFile2( File file,String fileName,String rootPath,String userName, String fieldName) {

159   

160     System.out.println("正在保存文件:filename:"+fileName);

161     

162     //String path = ServletActionContext.getServletContext().getRealPath("/"+userName+"/"+fieldName+"/");

163      

164     // String path="d:\\xtwcoalupload\\gggg\\"+username+"\\"+field_name;

165     

166     String path=ServletActionContext.getServletContext().getRealPath(rootPath+"//"+fieldName+"//"); 

167     String filePath=path+"\\"+fileName;

168     

169     if(file!=null){

170      

171      File newFile=new File(new File(path),fileName);

172      

173      if(!newFile.getParentFile().exists()){   

174       

175       newFile.getParentFile().mkdir();   

176      }

177      try {

178       FileUtils.copyFile(file, newFile); //Struts2 Api

179      } catch (IOException e) {

180       e.printStackTrace();

181      }    

182      System.out.println("文件保存路径"+path);

183     }

184     else 

185      System.out.println("没有文件可以保存!");

186     

187     return filePath;

188    }

189    

190 }

 

你可能感兴趣的:(struts2)