java惯用组件之文件工具2 FileUtils

java常用组件之文件工具2 FileUtils

/**
*
*/
package cn.ccb.jstsccf.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

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

/**
* @author xihn
* 文件处理帮助类
*/
public class FileUtil {

private static final Log log = LogFactory.getLog(FileUtil.class);

public static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}

protected static HttpServletResponse getResponse() {
return (HttpServletResponse) getExternalContext()
.getResponse();
}

private static ExternalContext getExternalContext() {
return getFacesContext().getExternalContext();
}

/**
* 判断文件是否为ZIP文件
*
* @param file
* @return
*/
public static boolean isZipFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("zip")) {
return true;
}
return false;
}

/**
* 判断文件是否为excel文件
*
* @param file
* @return
*/
public static boolean isExcelFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("xls")) {
return true;
}
return false;
}

/**
* 判断文件是否为Rar文件
*
* @param file
* @return
*/
public static boolean isRarFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("rar")) {
return true;
}
return false;
}

/**
* 判断文件是否为txt文件
*
* @param file
* @return
*/
public static boolean isTxtFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("txt")) {
return true;
}
return false;
}

/**
* 格式化的时分秒时间
*
* @return
*/
public static String fileDateFormat() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
return sdf.format(date);
}

/**
* 读文件并把文件的每一行放入List中
*
* @param filePath
* @return
* @throws IOException
*/
public static List readFile(InputStream is) throws IOException {

// 行List
List rowList = new ArrayList();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line = null;
while((line = br.readLine()) != null) {
if (StringUtils.isNotBlank(line)) {
rowList.add(line.trim());
}
}

return rowList;
}

public static void fileDownload(String fileName,File file){
InputStream is =null;
ServletOutputStream out=null;
try {
FacesContext ctx = getFacesContext();
ctx.responseComplete();
String contentType = "application/x-msdownload";
HttpServletResponse response = getResponse();
response.setContentType(contentType);
String s = "attachment; filename=\"" + URLEncoder.encode(fileName,"UTF-8") + "\"";

response.setHeader("Content-Disposition", s);
out = response.getOutputStream();

byte[] bytes = new byte[0xffff];
is = new FileInputStream(file);
int b = 0;
while ((b = is.read(bytes, 0, 0xffff)) > 0) {
out.write(bytes, 0, b);
}
ctx.responseComplete();

} catch (Exception e) {
try {
out.write("文件下载失败".getBytes());
} catch (IOException e1) {
log.error(e1);
}
//e.printStackTrace();
log.error("文件下载失败", e);
}finally{
try {
if(is!=null){
is.close();
}
if(out!=null){
out.flush();
}
} catch (IOException e) {
//e.printStackTrace();
log.error("文件下载失败", e);
}
}
}

}