package cloud.simple.service.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.commons.lang.StringUtils;
/****
* 文件操作類
* @version 1.0
*/
public class FileUtils extends org.apache.commons.io.FileUtils {
private static final int BUFFER_SIZE = 16 * 1024;
private static final long IMG_MAX_SIZE = 2 * 1024;
private static final String[] vidExt = new String[] { "rm", "rmvb", "mov",
"mtv", "dat", "wmv", "avi", "3gp", "amv", "dmv" };
private static final String[] imgExt = new String[] { "bmp", "png", "gif",
"jpeg", "jpg" };
private static final String[] docExt = new String[] { "doc", "docx" };
/**
* 刪除指定目錄下的所有文件
*
* @param folderPath
* 目錄路徑
* @return true:刪除成功 false:刪除失敗
*/
public static boolean delAllFile(String folderPath) {
boolean flag = false;
File file = new File(folderPath);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (folderPath.endsWith(File.separator)) {
temp = new File(folderPath + tempList[i]);
} else {
temp = new File(folderPath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(folderPath + "/" + tempList[i]);// 先刪除文件夾里面的文件
delFolder(folderPath + "/" + tempList[i]);// 再刪除空文件夾
flag = true;
}
}
return flag;
}
/**
* 刪除指定文件
*
* @param filePath
* 指定文件的路徑
* @return true:刪除成功 false:刪除失敗
*/
public static boolean delFile(String filePath) {
boolean flag = false;
File file = new File(filePath);
if (!file.exists()) {
return flag;
}
flag = (new File(filePath)).delete();
return flag;
}
/**
* 刪除指定文件夾(包括文件夾下的所有文件)
*
* @param folderPath
* 指定文件夾路徑
* @return true:刪除成功 false:刪除失敗
*/
public static boolean delFolder(String folderPath) {
try {
delAllFile(folderPath); // 刪除完里面所有內容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 刪除空文件夾
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 讀取文本文件的內容
*
* @param curfile
* 文本文件路徑
* @return 返回文件內容
*/
public static String readFile(String curfile) {
File f = new File(curfile);
try {
if (!f.exists())
throw new Exception();
FileReader cf = new FileReader(curfile);
BufferedReader is = new BufferedReader(cf);
StringBuilder filecontent = new StringBuilder("");
String str = is.readLine();
while (str != null) {
filecontent.append(str);
str = is.readLine();
if (str != null)
filecontent.append(System.getProperty("line.separator","\n"));
}
is.close();
cf.close();
return filecontent.toString();
} catch (Exception e) {
System.err.println("不能讀屬性文件: " + curfile + " \n" + e.getMessage());
return "";
}
}
/**
* 取指定文件的擴展名
*
* @param filePathName
* 文件路徑
* @return 擴展名
*/
public static String getFileExt(String filePathName) {
int pos = 0;
pos = filePathName.lastIndexOf('.');
if (pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
else
return "";
}
/**
* 讀取文件大小
*
* @param filename
* 指定文件路徑
* @return 文件大小
*/
public static int getFileSize(String filename) {
try {
File fl = new File(filename);
int length = (int) fl.length();
return length;
} catch (Exception e) {
return 0;
}
}
/**
* 文件拷貝
*
* @param src
* 源文件
* @param dst
* 目標文件
* @param delete
* 是否刪除源文件
*/
public static void copyFile(File src, File dst, boolean delete) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while ((n = in.read(buffer)) > 0) {
out.write(buffer,0,n);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
if (delete)
src.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取源文件類型
*
* @param ext
* @return
*/
public static String getSourceFileType(String ext) {
for (String img : imgExt) {
if (ext.equalsIgnoreCase(img))
return "IMG";
}
for (String doc : docExt) {
if (ext.equalsIgnoreCase(doc))
return "DOC";
}
for (String vid : vidExt) {
if (ext.equalsIgnoreCase(vid))
return "VID";
}
return null;
}
/**
* 檢查上傳文件有效性
*
* @param file
* @param fileName
* @param type
* 1、 圖片 2、文檔 3、音頻
* @return
*/
public static Object[] checkUploadFile(File file, String fileName,
Integer type) {
Object[] returns = new Object[] { true, "上傳文件有效。" };
if (file.length() > IMG_MAX_SIZE) {
returns[0] = false;
returns[1] = "上傳文件過大,請重新上傳。";
return returns;
}
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
// 上傳文件資源類型
String xtype = getSourceFileType(ext);
xtype = StringUtils.isNotEmpty(xtype) == true ? xtype : "UNKWON";
switch (type) {
case 1:
if (!xtype.equalsIgnoreCase("IMG")) {
returns[0] = false;
returns[1] = "上傳圖片文件錯誤,請重新上傳。格式為[bmp, png, gif, jpeg, jpg]";
}
break;
case 2:
if (!xtype.equalsIgnoreCase("DOC")) {
returns[0] = false;
returns[1] = "上傳文檔文件錯誤,請重新上傳。格式為[doc, docx]";
}
break;
case 3:
if (!xtype.equalsIgnoreCase("DOC")) {
returns[0] = false;
returns[1] = "上傳音頻文件錯誤,請重新上傳。格式為[rm,rmvb,mov,mtv,dat,wmv,avi,3gp,amv,dmv]";
}
break;
default:
break;
}
return returns;
}
public static void WriteFile(String file_name,String content,boolean append,boolean huanhang){
try{
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_name,append),"UTF-8"));
bw.write(content);
if(huanhang){
bw.newLine();
}
bw.flush();
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}