private String filePath = "D:\\message\\";
private String filePathZip = "D:\\messageZip\\Message.zip";
/**
* 文件下载
* @param response
* @param filePathZip
*/
private void downFile(HttpServletResponse response, String filePathZip) {
InputStream ins = null;
BufferedInputStream bins = null;
BufferedOutputStream bouts = null;
OutputStream outs = null;
try {
String path = filePathZip;
File file = new File(path);
if (file.exists()) {
//时间戳
String formatDate = DateHelper.formatDate(DateHelper.getCurrentDate(), "yyyyMMdd");
ins = new FileInputStream(path);
bins = new BufferedInputStream(ins);// 放到缓冲流里面
outs = response.getOutputStream();// 获取文件输出IO流
bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");// 设置response内容的类型
response.setHeader( "Content-disposition","attachment;filename="+ filePathZip.substring(14,21)+formatDate+filePathZip.substring(21));// 设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
outs.close();
} else {
throw new RuntimeException("要下载的文件生成错误");
}
} catch (IOException e) {
LogHelper.error(ACTION_NAME, e.toString());
}
finally{
if(outs!=null){
try {
outs.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
}
if(ins!=null){
try {
ins.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
}
if(bouts!=null){
try {
bouts.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
}
if(bins!=null){
try {
bins.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
}
}
}
/**
*
* @param inputFileName 输入一个文件夹
* @param zipFileName 输出一个压缩文件夹,打包后文件名字
* @throws Exception
*/
public void zip(String inputFileName, String zipFileName) throws Exception {
zip(zipFileName, new File(inputFileName));
}
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out=null;
File file =new File(zipFileName);
if(!file.exists()){
try {
boolean mkdirs = file.getParentFile().mkdirs();
boolean createNewFile = file.createNewFile();
if(!mkdirs&&!createNewFile){
throw new BusinessException("生成报文压缩文件错误");
}
} catch (IOException e) {
e.printStackTrace();
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
// 弹出提示信息,并回滚
putError("生成报文压缩文件错误");
}
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
try {
out = new ZipOutputStream(fileOutputStream);
zip(out, inputFile, "");
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(out!=null){
out.close();
}
}
}
private void zip(ZipOutputStream out, File f, String base) {
FileInputStream in = null;
try {
if (f.isDirectory()) { //判断是否为目录
File[] fl = f.listFiles();
out.putNextEntry(new java.util.zip.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 java.util.zip.ZipEntry(base));
in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
} catch (FileNotFoundException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
}
}
}