package com.wucl.stdmis.util;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 文件操作工具类
*
* @author wucl(lailaiwcl@gmail.com)
*
*/
public class FileUtil {
public static void downloadFile(File filepath, String downLoadFileName,
HttpServletResponse response) throws IOException {
if (filepath == null || !filepath.isFile()) {
throw new FileNotFoundException();
}
OutputStream outputStream = null;
InputStream in = null;
String fileName = "";
String name = filepath.getName();
if (downLoadFileName == null || downLoadFileName.trim().length() == 0) {
fileName = name;
} else {
String suffix = name.substring(name.lastIndexOf("."));
fileName = downLoadFileName + suffix;
}
try {
response.reset();
response.resetBuffer();
response.setCharacterEncoding("utf-8");
response.addHeader("Connection", "close");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));
response.addHeader("Content-Type", "application/octet-stream");
int fileLeng = Integer.parseInt(filepath.length() + "");
response.setContentLength(fileLeng);
outputStream = response.getOutputStream();
in = new BufferedInputStream(new FileInputStream(filepath));
copy(in, outputStream);
outputStream.flush();
} catch (IOException e) {
throw new IOException();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
public static void uploadFile(HttpServletRequest request,
HttpServletResponse response, File uploadFile) {
}
public static void copy(InputStream input, OutputStream output)
throws IOException {
final int DEFAULT_BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
}
private static final String OS_NAME = System.getProperty("os.name")
.toLowerCase(Locale.US);
public static final FileFilter LIST_FILE = new FileFilter() {
public boolean accept(File file) {
if (file == null) {
return false;
}
if (!file.isDirectory()) {
return true;
}
return false;
}
};
public static final FileFilter LIST_DIR = new FileFilter() {
public boolean accept(File file) {
if (file == null) {
return false;
}
if (file.isDirectory()) {
return true;
}
return false;
}
};
public static final IZipEntryFilter LIST_ZIP_ENTRY_FILE = new IZipEntryFilter() {
public boolean accept(ZipEntry zipEntry) {
if (zipEntry == null) {
return false;
}
if (!zipEntry.isDirectory()) {
return true;
}
return false;
}
};
public static final IZipEntryFilter LIST_ZIP_ENTRY_DIR = new IZipEntryFilter() {
public boolean accept(ZipEntry zipEntry) {
if (zipEntry == null) {
return false;
}
if (zipEntry.isDirectory()) {
return true;
}
return false;
}
};
/**
* 安静的关闭
*
* @param ioObj
*/
public static void closeQuietly(Object ioObj) {
if (ioObj == null) {
return;
}
if (ioObj instanceof Closeable) {
try {
((Closeable) ioObj).close();
return;
} catch (Throwable ignore) {
}
} else {
try {
Method method = ioObj.getClass().getMethod("close",
new Class[0]);
if (method != null) {
method.invoke(ioObj, new Object[0]);
return;
}
} catch (Throwable ignore) {
}
throw new IllegalArgumentException("ioObj'" + ioObj.getClass()
+ "' is not support type!");
}
}
/**
* 是否是绝对路径(根据当前系统判断)
*
* @param path
* 路径
* @return true:是
*/
public static boolean isAbsolutePath(String path) {
if (path == null || path.trim().length() == 0) {
throw new IllegalArgumentException("path is null!");
}
path = normalizeInUnixStyle(path);
if (isWindowsAndDos()) {
int colon = path.indexOf(":/");
return Character.isLetter(path.charAt(0)) && colon == 1;
} else {
return path.charAt(0) == '/';
}
}
private static boolean isWindowsAndDos() {
if (OS_NAME.indexOf("windows") > -1) {
return true;
}
if (OS_NAME.indexOf("dos") > -1) {
return true;
}
if (OS_NAME.indexOf("netware") > -1) {
return true;
}
return false;
}
/**
* 取得文件名
*
* @param filePath
* 文件路径
* @param isWithFileExtension
* 是否包含文件扩展名
* @return 文件名
*/
public static String getFileName(String filePath,
boolean isWithFileExtension) {
if (filePath == null || filePath.trim().length() == 0) {
throw new IllegalArgumentException("file is null!");
}
filePath = filePath.trim();
int index = filePath.lastIndexOf('/');
int index2 = filePath.lastIndexOf('\\');
if (index2 > index) {
index = index2;
}
String fileName = filePath;
if (index != -1) {
fileName = filePath.substring(index + 1);
}
if (!isWithFileExtension) {
index = fileName.lastIndexOf('.');
if (index != -1) {
fileName = fileName.substring(0, index);
}
}
return fileName.trim();
}
/**
* 取得文件扩展名
*
* @param filePath
* 文件路径
* @return 文件扩展名
*/
public static String getFileExtension(String filePath) {
if (filePath == null || filePath.trim().length() == 0) {
return null;
}
filePath = filePath.trim();
int index = filePath.lastIndexOf('.');
if (index != -1) {
return filePath.substring(index + 1).trim();
}
return null;
}
/**
* 路径是否匹配
*
* @param patternPath
* 路径模式
* @param path
* 路径
* @param isCaseSensitive
* 是否大小写敏感
* @return true:是
*/
public static boolean isMatch(String patternPath, String path,
boolean isCaseSensitive) {
char[] patArr = patternPath.toCharArray();
char[] strArr = path.toCharArray();
int patIdxStart = 0;
int patIdxEnd = patArr.length - 1;
int strIdxStart = 0;
int strIdxEnd = strArr.length - 1;
char ch;
boolean containsStar = false;
for (int i = 0; i < patArr.length; i++) {
if (patArr[i] == '*') {
containsStar = true;
break;
}
}
if (!containsStar) {
// No '*'s, so we make a shortcut
if (patIdxEnd != strIdxEnd) {
return false; // Pattern and string do not have the same size
}
for (int i = 0; i <= patIdxEnd; i++) {
ch = patArr[i];
if (ch != '?') {
if (isCaseSensitive && ch != strArr[i]) {
return false; // Character mismatch
}
if (!isCaseSensitive
&& Character.toUpperCase(ch) != Character
.toUpperCase(strArr[i])) {
return false; // Character mismatch
}
}
}
return true; // String matches against pattern
}
if (patIdxEnd == 0) {
return true; // Pattern contains only '*', which matches anything
}
// Process characters before first star
while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {
if (ch != '?') {
if (isCaseSensitive && ch != s