@At
@Ok("raw:stream")
@Permit(url = "/web/monitor/reportBillOrder/index")
public void exportExcel(@Param("q") String q, HttpServletResponse response) throws UnsupportedEncodingException {
QueryBillVo queryBillVo = Json.fromJson(QueryBillVo.class, q);
Map<String, List<HSSFWorkbook>> retMap = billNewService.exportBillOrder(queryBillVo);
response.setHeader("content-disposition",
"attachment;filename=" + URLEncoder.encode(DateUtils.toDate(new Date(), "yyyyMMddHHmmss") + ".gz", "UTF-8"));
OutputStream out = null;
try {
String fd = DateUtils.getCurTime3();
String fdPath = ZIP_TMP_PATH + fd;
// 出力到文件夹
for (Map.Entry<String, List<HSSFWorkbook>> entry : retMap.entrySet()) {
String eptName = entry.getKey();
// 创建文件夹
File f = new File(fdPath);
if (!f.exists()) {
f.mkdirs();
}
// 创建文件
String fileName = eptName + ".zip";
File zipFile = new File(f, fileName);
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
LOG.error(e.getMessage(),e);
e.printStackTrace();
}
}
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
List<HSSFWorkbook> wbLst = entry.getValue();
for (HSSFWorkbook workbook : wbLst) {
ZipEntry zipEntry = new ZipEntry(eptName+ "_" + DateUtils.getCurTime3() + ".xls");
zipOut.putNextEntry(zipEntry);
workbook.write(zipOut);
}
zipOut.flush();
zipOut.close();
}
// 创建 response 输出流
out = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
// 读取文件
File file = new File(fdPath);
if (file.isDirectory()) {
File[] files = file.listFiles();
InputStream input = null;
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream(files[i]);
zip.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zip.write(temp);
}
input.close();
}
}
zip.close();
deleteDir(new File(fdPath));
} catch (IOException e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
@Ok("raw:stream")
@Permit(url = "/web/monitor/reportBillOrder/index")
public void exportExcel(@Param("q") String q, HttpServletResponse response) throws UnsupportedEncodingException {
QueryBillVo queryBillVo = Json.fromJson(QueryBillVo.class, q);
Map<String, List<HSSFWorkbook>> retMap = billNewService.exportBillOrder(queryBillVo);
response.setHeader("content-disposition",
"attachment;filename=" + URLEncoder.encode(DateUtils.toDate(new Date(), "yyyyMMddHHmmss") + ".gz", "UTF-8"));
OutputStream out = null;
try {
String fd = DateUtils.getCurTime3();
String fdPath = ZIP_TMP_PATH + fd;
// 出力到文件夹
for (Map.Entry<String, List<HSSFWorkbook>> entry : retMap.entrySet()) {
String eptName = entry.getKey();
// 创建文件夹
File f = new File(fdPath);
if (!f.exists()) {
f.mkdirs();
}
// 创建文件
String fileName = eptName + ".zip";
File zipFile = new File(f, fileName);
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
LOG.error(e.getMessage(),e);
e.printStackTrace();
}
}
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
List<HSSFWorkbook> wbLst = entry.getValue();
for (HSSFWorkbook workbook : wbLst) {
ZipEntry zipEntry = new ZipEntry(eptName+ "_" + DateUtils.getCurTime3() + ".xls");
zipOut.putNextEntry(zipEntry);
workbook.write(zipOut);
}
zipOut.flush();
zipOut.close();
}
// 创建 response 输出流
out = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
// 读取文件
File file = new File(fdPath);
if (file.isDirectory()) {
File[] files = file.listFiles();
InputStream input = null;
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream(files[i]);
zip.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zip.write(temp);
}
input.close();
}
}
zip.close();
deleteDir(new File(fdPath));
} catch (IOException e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}