近段时间在做一个项目,其中有一个功能是需要将用户填的信息以及上传的附件等进行打包下载。于是乎,网上各种找资料,现将成果分享如下:
/**
* 打包下载文件 信息
* @param mav
* @param request
* @param response
* @param attachmentId
*/
@Transactional(propagation=Propagation.REQUIRED, readOnly = true)
public void zipDownLoadAttachment(ModelAndView mav, HttpServletRequest request,Map result,HttpServletResponse response,String id){
List listAttachment=supplierSubAttachmentDao.findByChiefId(Integer.valueOf(id));
List listContact=supplierSubContactDao.findByChiefId(Integer.valueOf(id));
List listTrack=supplierSubTrackDao.findByChiefId(Integer.valueOf(id));
SupplierChiefEntity chief=supplierChiefDao.findById(Integer.valueOf(id));
//导出基础数据
this.outPutExcel(request,chief,listContact,listTrack);
String filePath=BusSystemConfig.downLoadPath;
String path=BusSystemConfig.contentFileUpLocalPath;
// //生成的ZIP文件名为Demo.zip
String tmpFileName = chief.getName()+".zip";
byte[] buffer = new byte[1024];
String strZipPath = filePath +tmpFileName;
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
strZipPath));
String filename="";
// 需要同时下载的多个文件
File[] file1=new File[listAttachment.size()+1];
for(int i=0;i 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.flush();
out.close();
this.downLoad(mav,request,response,tmpFileName,filePath);
} catch (Exception e) {
logger.error("文件下载出错", e);
}finally{
try {
File f = new File(filePath+chief.getName()+".xls");
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//这段代码是将压缩包下载到本地
public void downLoad(ModelAndView mav,HttpServletRequest request,HttpServletResponse response,String str,String filePath){
String path=filePath+str;
try {
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
OutputStream os = response.getOutputStream();
response.reset();// 清空输出流
response.setContentType(request.getSession().getServletContext().getMimeType(str));
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
response.setHeader("Content-disposition", "attachment;filename=" + new String(str.getBytes("UTF-8"),"ISO8859-1"));
InputStream fis = new FileInputStream(path);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
os.write(buffer);// 输出文件
os.flush();
os.close();
} catch (IOException e) {
logger.error("文件下载出错", e);
} finally{
try {
File f = new File(path);
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void outPutExcel(HttpServletRequest request,SupplierChiefEntity chiefEntity, List listContact,List listTrack) {
try {
String fileName = chiefEntity.getName()+".xls";
String filePath=BusSystemConfig.downLoadPath;
WritableWorkbook workbook = Workbook.createWorkbook(new File(filePath+fileName));
if (workbook != null) {
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定义格式
WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
WritableCellFormat wcfmt = new WritableCellFormat(); // 单元格定义
// 设置标题 sheet.addCell(new jxl.write.Label(列(从0开始), 行(从0开始),
// 内容.));
try {
String[] baseTitles=baseTitle();
wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK);
wcfmt.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK);
WritableSheet sheet = workbook.createSheet("基础信息", 0);
sheet.addCell(new Label(0, 0, "基础信息", wcf));
// 合并单元格
sheet.mergeCells(0, 0, baseTitles.length-1, 0);
// 设置行高度
sheet.setRowView(0, 500);
for(int i=0;i