1 package com.xyworkroom.ntko.util; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * 文件处理工具类 14 * 15 * @author xmq 16 */ 17 public class FileUtil { 18 19 /** 20 * 下载文件 21 * @param response 22 * @param filePat 包括文件名如:c:/a.txt 23 * @param fileName 文件名如:a.txt 24 */ 25 public static void downFile(HttpServletResponse response,String filePath,String fileName){ 26 try { 27 response.setCharacterEncoding("gkb"); 28 response.setContentType("text/plain"); 29 response.setHeader("Location",fileName); 30 response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"),"ISO8859-1")); 31 FileInputStream fis=new FileInputStream(filePath); 32 OutputStream os=response.getOutputStream(); 33 byte[] buf=new byte[1024]; 34 int c=0; 35 while((c=fis.read(buf))!=-1){ 36 os.write(buf, 0, c); 37 } 38 os.flush(); 39 os.close(); 40 if(fis!=null){ 41 fis.close(); 42 } 43 } catch (Exception e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 } 48 /** 49 * 检查文件是否存在,存在返回true 50 * @param destFileName 51 * @return 52 */ 53 public static boolean checkFileIsExists(String destFileName){ 54 File file = new File(destFileName); 55 if (file.exists()) { 56 return true; 57 }else{ 58 return false; 59 } 60 } 61 /** 62 * 复制文件 63 * @param source 64 * @param dest 65 * @throws IOException 66 */ 67 public static void copyFile(File source, File dest){ 68 InputStream input = null; 69 OutputStream output = null; 70 try { 71 input = new FileInputStream(source); 72 output = new FileOutputStream(dest); 73 byte[] buf = new byte[1024]; 74 int bytesRead; 75 while ((bytesRead = input.read(buf))>-1) { 76 output.write(buf, 0, bytesRead); 77 } 78 output.close(); 79 input.close(); 80 }catch(Exception e){ 81 e.printStackTrace(); 82 } 83 } 84 85 /** 86 * 把输入流保存到指定文件 87 * @param source 88 * @param dest 89 * @throws IOException 90 */ 91 public static void saveFile(InputStream source, File dest){ 92 InputStream input = null; 93 OutputStream output = null; 94 try { 95 input =source; 96 output = new FileOutputStream(dest); 97 byte[] buf = new byte[1024]; 98 int bytesRead; 99 while ((bytesRead = input.read(buf))>-1) { 100 output.write(buf, 0, bytesRead); 101 } 102 output.close(); 103 input.close(); 104 }catch(Exception e){ 105 e.printStackTrace(); 106 } 107 } 108 /** 109 * 创建文件 110 */ 111 public static boolean createFile(String destFileName) { 112 File file = new File(destFileName); 113 if (file.exists()) { 114 return false; 115 } 116 if (destFileName.endsWith(File.separator)) { 117 return false; 118 } 119 if (!file.getParentFile().exists()) { 120 if (!file.getParentFile().mkdirs()) { 121 return false; 122 } 123 } 124 try { 125 if (file.createNewFile()) { 126 return true; 127 } else { 128 return false; 129 } 130 } catch (IOException e) { 131 e.printStackTrace(); 132 return false; 133 } 134 } 135 136 /** 137 * 创建目录 138 */ 139 public static boolean createDir(String destDirName) { 140 File dir = new File(destDirName); 141 if (dir.exists()) { 142 return false; 143 } 144 if (!destDirName.endsWith(File.separator)) 145 destDirName = destDirName + File.separator; 146 if (dir.mkdirs()) { 147 return true; 148 } else { 149 return false; 150 } 151 } 152 153 /** 154 * 根据路径删除指定的目录或文件,无论存在与否 155 */ 156 public static boolean DeleteFolder(String sPath) { 157 boolean flag = false; 158 File file = new File(sPath); 159 if (!file.exists()) { 160 return flag; 161 } else { 162 if (file.isFile()) { 163 return deleteFile(sPath); 164 } else { 165 return deleteDirectory(sPath); 166 } 167 } 168 } 169 170 /** 171 * 删除单个文件 172 */ 173 public static boolean deleteFile(String sPath) { 174 boolean flag = false; 175 File file = new File(sPath); 176 if (file.isFile() && file.exists()) { 177 file.delete(); 178 flag = true; 179 } 180 return flag; 181 } 182 183 /** 184 * 删除目录(文件夹)以及目录下的文件 185 */ 186 public static boolean deleteDirectory(String sPath) { 187 if (!sPath.endsWith(File.separator)) { 188 sPath = sPath + File.separator; 189 } 190 File dirFile = new File(sPath); 191 if (!dirFile.exists() || !dirFile.isDirectory()) { 192 return false; 193 } 194 boolean flag = true; 195 File[] files = dirFile.listFiles(); 196 for (int i = 0; i < files.length; i++) { 197 if (files[i].isFile()) { 198 flag = deleteFile(files[i].getAbsolutePath()); 199 if (!flag) 200 break; 201 } else { 202 flag = deleteDirectory(files[i].getAbsolutePath()); 203 if (!flag) 204 break; 205 } 206 } 207 if (!flag) 208 return false; 209 if (dirFile.delete()) { 210 return true; 211 } else { 212 return false; 213 } 214 } 215 216 217 /*public static void main(String[] args) { 218 String dir = "D:\\sgtsc_files\\393\\02\\"; 219 createDir(dir); 220 String filename = "test1.txt"; 221 String subdir = "subdir"; 222 createDir(dir + subdir); 223 createFile(dir + filename); 224 createFile(dir + subdir + filename); 225 DeleteFolder(dir); 226 }*/ 227 228 }
时间: 2024-10-21 15:47:17