六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 64|回复: 0

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

[复制链接]

升级  86%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
43
 楼主| 发表于 2013-1-27 12:53:22 | 显示全部楼层 |阅读模式
/**
*
*/
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);
   }
  }
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表