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
原创粉丝点击