import java.io.File;java import java.io.FileInputStream;app import java.io.FileOutputStream;url import java.io.IOException;spa import java.io.InputStream;excel import java.io.OutputStream;對象 import javax.servlet.http.HttpServletResponse;ci public class FileHelper {get static File file ; input /**servlet * @刪除文件 * input 輸入 output 輸出 * */ public static boolean deleteFile(String url){ boolean flag = false; try{ if(null == file){ file = new File(url); if(file.isFile()){ if(file.canWrite()){ file.delete(); } flag = true; } }else{ if(url == file.getPath()){ file.delete(); flag = true; } } }catch(Exception e){ flag = false ; e.printStackTrace(); }finally{ file = null; System.gc(); } return flag; }
/** * 新建目錄 * @param folderPath String 如 c:/fqf * @return boolean */ public static void newFolder(String folderPath) { try { String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { e.printStackTrace(); } }
/** * 複製單個文件 * @param oldPath String 原文件路徑 如:c:/fqf.txt * @param newPath String 複製後路徑 如:f:/fqf.txt * @return boolean */ public static void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在時 InputStream inStream = new FileInputStream(oldPath); //讀入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字節數 文件大小 fs.write(buffer, 0, byteread); } inStream.close(); fs.close(); } } catch (Exception e) { e.printStackTrace(); } } /**
* 使用文件通道的方式複製文件 * @param s 源文件 * @param t 複製到的新文件 */ public static void fileChannelCopy(String srcPath,String outpath) { File s = new File(srcPath); File t = new File(outpath); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();//獲得對應的文件通道 out = fo.getChannel();//獲得對應的文件通道 in.transferTo(0, in.size(), out);//鏈接兩個通道,而且從in通道讀取,而後寫入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 上傳文件 * @param upFile 上傳文件 * @param targetFile 目標文件 * @param b * @return */ public static boolean fileUpload(File upFile , File targetFile , byte[] b){ InputStream is = null ; OutputStream os = null ; try{ is = new FileInputStream(upFile); os= new FileOutputStream(targetFile); int length = 0; while((length = is.read(b))>0){ os.write(b, 0, length); } }catch(Exception e){ e.printStackTrace(); return false; }finally{ try { if(null != is){ is.close(); }if(null != os){ os.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * 刪除文件 * @param f * @return */ public static boolean deleteFile(File f){ if (f.isFile()) f.delete(); return true; }
/** * 刪除文件夾 * @param f * @return */ public static boolean deleteDir(File f){ if(f.isDirectory()){ File[] files = f.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()) deleteDir(files[i]); else deleteFile(files[i]); } } f.delete(); return true ; }
/** * 文件下載 * @param filePath 文件路徑 * @param fileType 文件類型 * @param downName 下載文件名稱 * @param response 響應 */ public static void downLoadFile(String filePath,String fileType ,String downName,HttpServletResponse response){ OutputStream out = null;// 建立一個輸出流對象 String headerStr = downName; try{ InputStream is=new FileInputStream(filePath); out = response.getOutputStream();// headerStr =new String( headerStr.getBytes("gb2312"), "ISO8859-1" );//headerString爲中文時轉碼 response.setHeader("Content-disposition", "attachment; filename="+ headerStr+"."+fileType);// filename是下載的xls的名,建議最好用英文 response.setContentType("application/msexcel;charset=GB2312");// 設置類型 response.setHeader("Pragma", "No-cache");// 設置頭 response.setHeader("Cache-Control", "no-cache");// 設置頭 response.setDateHeader("Expires", 0);// 設置日期頭 // 輸入流 和 輸出流 int len = 0; byte[] b = new byte[1024]; while ((len = is.read(b)) != -1) { out.write(b, 0, len); } is.close(); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
} |