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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import com.bcap_systemlib.config.Configuration;
public class FileUtil {
/**
* 将多个文件打包到一个zip中
*
* @param sourceFolder
* @param zipFile
* @return
* @throws Exception
*/
public static boolean zipFile(String sourceFolder, File zipFile) throws Exception{
boolean isOk = true;
File f = new File(sourceFolder);
ZipOutputStream out = null;
try{
if(!f.exists()){
f.mkdirs();
}
out = new ZipOutputStream(new FileOutputStream(zipFile));
zip(out, f, "");
out.flush();
FileUtils.deleteDirectory(f);
} catch (Exception e){
e.printStackTrace();
throw new Exception("压缩文件出错!");
} finally
{
if(null != out){
try{ out.close(); } catch (Exception e){ e.printStackTrace();}
}
}
return isOk;
}
/**
* 递归压缩文件
* @param out
* @param f
* @param base
* @throws Exception
*/
private static void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* 下载单个文件
*
* @param file
* @param request
* @param response
* @return
*/
public static boolean downFile(File file, HttpServletRequest request, HttpServletResponse response) {
boolean isOk = true;
OutputStream myout = null;
FileInputStream fis = null;
BufferedInputStream buff = null;
HttpSession session = request.getSession();
if (session != null) {
session.setAttribute("state", "");
}
try {
response.setContentType("application/x-msdownload");
response.setContentLength((int) file.length());
response.setHeader("content-disposition", "attachment;filename=" + EncodingConvertUtil.utf2iso(file.getName()));
fis = new FileInputStream(file);
buff = new BufferedInputStream(fis);
byte[] b = new byte[1024 * 10];//相当于我们的缓存
long k = 0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, b.length);
k += j;
//将b中的数据写到客户端的内存
myout.write(b, 0, j);
}
myout.flush();
} catch (Exception e) {
e.printStackTrace();
isOk = false;
} finally {
try {
if (null != myout) {
myout.close();
myout = null;
}
if (null != buff) {
buff.close();
buff = null;
}
if (null != fis) {
fis.close();
fis = null;
}
if(file.exists()){
FileUtil.delFile(file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return isOk;
}
/**
* 删除单个文件
*
* @param file
* @return
*/
public static boolean delFile(File file) {
boolean isOk = true;
try {
if (file.isFile() && file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
isOk = false;
} finally {
// log ...
}
return isOk;
}
public static void downloadFileFromRemote(String remoteFilePath, String localFilePath){
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(localFilePath);
try
{
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
int len = 2048;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1)
{
bos.write(b, 0, len);
}
System.out.println("下载成功");
bos.flush();
bis.close();
httpUrl.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
bis.close();
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void writeFile(String filePathAndName, String fileContent) {
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),Configuration.SERVLET_CHARACTER_ENCODING);
BufferedWriter writer=new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("写文件内容操作出错");
e.printStackTrace();
}
}
public static String readFile(String filePathAndName) {
String fileContent = "";
try {
File f = new File(filePathAndName);
if(f.isFile()&&f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f),Configuration.SERVLET_CHARACTER_ENCODING);
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return fileContent;
}
public static String getExtName(String filename) {
int index = filename.lastIndexOf(".");
if (index == -1) {
return null;
}
String result = filename.substring(index);
return result;
}
}