ZIPFILE

/**
* 下载-将多个文件压缩成zip
*
* @author penz
* @param srcfile 文件列表File
* @param zipfile 文件名的File
*/
public static void zipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (Exception e) {
log.error("ExportController.zipFiles 异常: {}", e);
e.printStackTrace();
}
}


package com.kame.micropay.order.service.utils;

import java.util.ArrayList;
import java.util.List;

/**
* 说明: List相关工具类
*
* @author penz @date 2013-10-18
*/
public class ListUtil {

/**
* 将list分成多个List
*
* @author penz
* @param list 集合对象
* @param pageSize 每个集合大小数
* @return
*/
public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
int listSize = list.size();
int page = (listSize + (pageSize - 1)) / pageSize;
List<List<T>> listArray = new ArrayList<List<T>>();
for (int i = 0; i < page; i++) {
List<T> subList = new ArrayList<T>();
for (int j = 0; j < listSize; j++) {
int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
if (pageIndex == (i + 1)) {
subList.add(list.get(j));
}
if ((j + 1) == ((i + 1) * pageSize)) {
break;
}
}
listArray.add(subList);
}
return listArray;
}
}


/**
* 输出报表
* @param response
* @param data
*/
public static void reportExp(HttpServletResponse response, String templateUrl, Map<String, Object> data) {
OutputStream fOut = null;
InputStream fin = null;
try {
URL url = new URL(templateUrl);
URLConnection connection = url.openConnection();
fin = connection.getInputStream();
fOut = response.getOutputStream();

XLSTransformer transformer = new XLSTransformer();
Workbook workbook = transformer.transformXLS(fin, data);
workbook.write(fOut);
} catch (Exception e) {
log.error("报表导出异常", e);
} finally {
try {
if (fOut != null) {
fOut.flush();
fOut.close();
}
if (fin != null) {
fin.close();
}
} catch (IOException ie) {
log.error("报表导出异常", ie);
}
}
}

/**
* 下载
*
* @author penz
* @param bais ByteArrayInputStream对象
* @param fileName 文件名
* @param response HttpServletResponse对象
*/
public static void download(InputStream bais, String fileName, HttpServletResponse response) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
response.setCharacterEncoding("UTF-8");
OutputStream out = response.getOutputStream();
response.setContentType("application/x-download");
response.addHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
bis = new BufferedInputStream(bais);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
log.error("ExportController.download 异常: {}", e);
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值