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();
}
}
}
}

猜你喜欢

转载自tangkuo.iteye.com/blog/2287104