java先下载excel文件,然后与其它文件、图片压缩
来源:互联网 发布:好玩又不要网络的游戏 编辑:程序博客网 时间:2024/06/03 18:35
String path = "/static/template/信息导出模板.xls"; HSSFWorkbook work = new HSSFWorkbook(new FileInputStream(path)); //下载xls文件 response.setContentType("application/x-download"); String xlsFileName = "‘" + maininfo.getVname() + "’" + "的单位信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xls"; response.setHeader("Content-disposition","attachment;filename=" + new String(xlsFileName.getBytes("utf-8"), "iso-8859-1"));//中文转码 response.setCharacterEncoding("utf-8"); FileOutputStream fos = new FileOutputStream( xlsFileName); work.write(fos); fos.close(); String fileName = "‘" + maininfo.getVname() + "’" + "的单位信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".zip";//压缩后文件的名称 String zipFilePath = “”;//压缩后存放路径 FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; File zipFile = new File(zipFilePath + "/" + fileName); if(zipFile.exists()){ System.out.println(zipFilePath + "目录下存在名字为:" + fileName +" 的打包文件."); }else{ practice.setVid(vid); List<CompanyPractice> practiceList = dao.getCompanyPractice(practice); //获取文件/图片的路径并处理压缩 List<String> fileList = Lists.newArrayList(); for (int i = 0; i < practiceList.size(); i++) { String files = practiceList.get(i).getFiles(); if(!"".equals(files) && files != null){//演练资料 String[] filesStrs = files.substring(files.indexOf("/")).split("\\|"); for (int j = 0; j < filesStrs.length; j++) { if(!"".equals(filesStrs[j]) && filesStrs[j] != null){ fileList.add(webapps + URLDecoder.decode(filesStrs[j],"UTF-8"));//中文转码 } } } } fileList.add(xlsFileName);//excel文件 String []sourceFiles = quChong(fileList);//去重,List转String[] //开始压缩文件 fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024*10]; for(int i=0;i<sourceFiles.length;i++){ File newFile = new File(sourceFiles[i]); //重名文件 System.currentTimeMillis(); StringBuilder builder = new StringBuilder(newFile.getName()); builder.insert(newFile.getName().indexOf("."), "_"+i); //创建ZIP实体,并添加进压缩包 zos.putNextEntry(new ZipEntry(builder.toString())); //读取待压缩的文件并写进压缩包里 fis = new FileInputStream(sourceFiles[i]); bis = new BufferedInputStream(fis, 1024*10); int len = 0; while((len=bis.read(bufs, 0, 1024*10)) != -1){ zos.write(bufs,0,len); } zos.closeEntry(); bis.close(); } zos.close(); //将压缩文件下载 InputStream ins = new FileInputStream(zipFilePath + "/" + fileName); BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面 OutputStream outs = response.getOutputStream();// 获取文件输出IO流 BufferedOutputStream bouts = new BufferedOutputStream(outs); response.setContentType("application/x-download");// 设置response内容的类型 response.setHeader( "Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息 int bytesRead = 0; byte[] buffer = new byte[8192]; // 开始向网络传输文件流 while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) { bouts.write(buffer, 0, bytesRead); } bouts.flush();// 这里一定要调用flush()方法 ins.close(); bins.close(); outs.close(); bouts.close(); }
0 0
- java先下载excel文件,然后与其它文件、图片压缩
- java 下载Excel动态模板文件压缩包
- Java Excel文件下载
- Java下载Excel文件
- 把一个文件夹下的文件先打包成一个tar文件,然后再用gzip压缩。
- java导出Excel文件,直接可以下载,然后更新状态,刷新页面
- 在angularjs客户端压缩图片文件然后上传
- java多文件压缩下载
- java 文件压缩和下载
- 将DataGrid数据写入Excel文件,然后下载
- 图片文件压缩java代码
- java 等比压缩图片文件
- jquery与其它js文件冲突解决办法
- asp.net mvc导出excel文件先保存到服务器某个目录,然后再导出到客户端供客户下载功能的实现
- java-flex 批量下载文件-压缩下载
- java 下载样板Excel文件
- java 下载文件 例:excel
- Java文件下载(excel)
- Java之动态绑定(多态)
- 使用js实现用户评论的星级
- train\predict\cross_validation demo include almost all Classifier from scikit-learning
- Android多媒体整体架构图
- 开端-且做个年末总结吧
- java先下载excel文件,然后与其它文件、图片压缩
- awk 系统学习,转载
- 递归
- 图片测试
- android webview 在项目中的那些坑
- idea下svn项目
- CVPR 2016-12-07
- 常用宽高
- 【JAVA - SSM】之MyBatis插入数据后获取自增主键