天天看點

AttachmentHelper附件管理工具類

package com.hhh.platform.advisors.attachment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AttachmentHelper {
	private static Log log = LogFactory.getLog(AttachmentHelper.class);
	public static String SEP = "/";

	public static void copyStream(InputStream input, OutputStream output) throws Exception {
		byte[] buffer = new byte[1024 * 4];
		int n = 0;
		try {
			while ((n = input.read(buffer)) != -1) {
				output.write(buffer, 0, n);
			}
		} catch (IOException e) {
			int code = (int) System.currentTimeMillis();
			log.error("複制檔案流出錯(" + code + ")", e);
			throw new Exception("複制檔案流出錯", e);
		}
	}

	public static void writeStream(String str, OutputStream output) throws Exception {
		try {
			output.write(str.getBytes());
		} catch (IOException e) {
			int code = (int) System.currentTimeMillis();
			log.error("複制檔案流出錯(" + code + ")", e);
			throw new Exception("複制檔案流出錯", e);
		}
	}

	/**
	 * 
	 * @param dir
	 * @param fileName
	 * @param inputStream
	 *            是字元串或是 inputStream?
	 * @throws Exception
	 */
	public static void saveFile(String dir, String fileName, InputStream inputStream) throws Exception {
		FileOutputStream out = null;
		try {
			File file = new File(dir + AttachmentHelper.SEP + fileName);
			out = new FileOutputStream(file);
			copyStream(inputStream, out);
		} catch (Exception e) {
			int code = (int) System.currentTimeMillis();
			log.error("上傳檔案異常(" + code + ")", e);
			throw new Exception("上傳檔案異常!", e);
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (out != null) {
				out.close();
			}

		}

	}

	/**
	 * 擷取目錄下的所有檔案名稱
	 * 
	 * @param dir
	 * @return
	 * @throws Exception
	 */
	public static String[] listFiles(String dir) throws Exception {
		// 定位目錄
		String[] fileArray = null;
		try {
			File directory = new File(dir);
			if (!directory.exists()) {
				return null;
			} else {
				File[] files = directory.listFiles();
				fileArray = new String[files.length];
				if (fileArray.length > 0) {
					for (int i = 0; i < files.length; i++) {
						fileArray[i] = files[i].getName();
					}
				}
				return fileArray;
			}
		} catch (Exception e) {
			int code = (int) System.currentTimeMillis();
			log.error("擷取目錄下所有檔案異常(" + code + ")", e);
			throw new Exception("擷取目錄下所有檔案異常!", e);
		}

	}

	/**
	 * 擷取目錄下某個檔案
	 * 
	 * @param dir
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static InputStream getFileAsStream(String dir, String fileName) throws Exception {
		try {
			FileInputStream stream = new FileInputStream(dir + AttachmentHelper.SEP + fileName);
			return stream;
		} catch (Exception e) {
			int code = (int) System.currentTimeMillis();
			log.error("讀取檔案異常(" + code + ")", e);
			throw new Exception("讀取檔案異常!", e);
		}

	}

	/**
	 * 删除指定目錄下檔案
	 * 
	 * @param dir
	 * @param fileName
	 * @throws Exception
	 */
	public static void deleteFile(String dir, String fileName) throws Exception {
		try {
			File file = new File(dir + AttachmentHelper.SEP + fileName);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception e) {
			int code = (int) System.currentTimeMillis();
			log.error("删除檔案異常(" + code + ")", e);
			throw new Exception("附件不存在或删除失敗!", e);
		}
	}

	/**
	 * 删除指定目錄
	 * 
	 * @param dir_path
	 * @param fileName
	 * @throws Exception
	 */
	public static void deleteDir(String dir_path) throws Exception {
		try {
			File directory = new File(dir_path);
			if (directory.exists()) {
				if (directory.isDirectory()) {
					File[] files = directory.listFiles();
					if (files != null && files.length > 0) {
						for (int i = 0; i < files.length; i++) {
							files[i].delete();
						}
					}
				}
				directory.delete();
			}
		} catch (Exception e) {
			int code = (int) System.currentTimeMillis();
			log.error("删除目錄異常(" + code + ")", e);
			throw new Exception("删除目錄失敗!", e);
		}
	}

}