在浏览器端点击下载,会下载一个zip压缩包,里面包含多个Excel文件(二)
我暂且把从程序中下载压缩包分为三种类型,即三步下载,两步下载,一步下载。三步下载是指第一步先从数据库读取数据、写成文件,然后把文件们下载到本地磁盘;第二步是把文件们打成压缩包;第三步是把压缩包读取到程序中然后响应到浏览器。两步下载是指从数据库读取数据、写成文件再打成压缩包,然后把压缩包下载到本地磁盘,这是第一步;第二步是把压缩包读取到程序中然后响应到浏览器。一步下载是指程序从数据库读取数据、写成文件、转成流和响应到浏览器,都不用写到本地磁盘,只在内存中,一步输出压缩包。
本次先以多Excel文件打成Zip压缩包为例,其他文件格式后续发表。
2.两步下载
特点:把多excel流直接生成到zip实体中,然后把压缩包保存到本地;把压缩包响应到浏览器
优点:比三步下载少下载Excel文件,只需要把zip流输出到本地
难点:ZipEntry的特性
第一步:在程序内生成Excel文件,把数据流写入到zip实体中,把zip输出到本地磁盘(有标注:第一步);
第二步:在磁盘读取zip文件,把文件流响应到浏览器端(有标注:第二步);
public static boolean fileToZip(List<byte[]>bytes, String zipFilePath,
String fileName) {
boolean flag = false;
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
System.out.println(zipFilePath + "目录下存在名字为:" + fileName
+ ".zip" + "打包文件.");
} else {
if(!zipFile.exists()){
zipFile.getParentFile().mkdirs();
}//第一步
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
if(bytes!=null&&bytes.size()>0)
for(int i=0;i<bytes.size();i++){
byte[] b=bytes.get(i);
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(i+".xls");
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
zos.write(b);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 关闭流
try {
if (null != zos)
zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return flag;
}
public static void createExcel(ByteOutputStream bytes) throws Exception {
// /创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(bytes);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label xuexiao = new Label(0, 0, "学校");
sheet.addCell(xuexiao);
Label zhuanye = new Label(1, 0, "专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2, 0, "专业竞争力");
sheet.addCell(jingzhengli);
Label qinghua = new Label(0, 1, "清华大学");
sheet.addCell(qinghua);
Label jisuanji = new Label(1, 1, "计算机专业");
sheet.addCell(jisuanji);
Label gao = new Label(2, 1, "高");
sheet.addCell(gao);
Label beida = new Label(0, 2, "北京大学");
sheet.addCell(beida);
Label falv = new Label(1, 2, "法律专业");
sheet.addCell(falv);
Label zhong = new Label(2, 2, "中");
sheet.addCell(zhong);
Label ligong = new Label(0, 3, "北京理工大学");
sheet.addCell(ligong);
Label hangkong = new Label(1, 3, "航空专业");
sheet.addCell(hangkong);
Label di = new Label(2, 3, "低");
sheet.addCell(di);
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
bytes.close();
}
public static void main(String[] args) throws Exception {
ByteOutputStream bytes = new ByteOutputStream();
ByteOutputStream bytes1 = new ByteOutputStream();
createExcel(bytes);
createExcel(bytes1);
List<byte[]> listBytes = new ArrayList<byte[]>();
byte[] b = bytes.getBytes();
byte[] b1= bytes1.getBytes();
listBytes.add(b);
listBytes.add(b1);
String zipFilePath = "F:\\update";
String fileName = "tp-admin";
fileToZip(listBytes, zipFilePath, fileName);
}
//第二步
//读取zip文件,并下载到浏览器
File file = new File(filePath + "/" + fileName);
fis = new FileInputStream(file);
byte [] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream ous = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
ous.write(buffer);
ous.flush();
ous.close();
==================================================================