java打包下载

-------------------------------------------------后端-------------------------------------------------  

 /**
     * 图片下载
     */
    @RequestMapping("/getTestinfoPhoto")
//    public @VoidResponseBody void exportOutUserInfo(HttpServletResponse response, HttpServletRequest request)
//            throws IOException {
//        //接收前台传来的参数
//        String paths = request.getParameter("paths");
//        //参数格式是文件地址+;;+文件名称
//        //将参数拆分成地址+文件名
//        String str[] = paths.split(";;");
//        String filepath="";
//        String fileName="";
//        //将文件名拆分出来
//        String photoName[] = str[1].split(";");
//        // 清空response
//        response.reset();
//        for(int i = 0; i<photoName.length ;i++){
//            filepath = str[0];//文件地址
//            fileName = photoName[i];//文件名
//            String savePath = request.getSession().getServletContext().getRealPath("/");
//            //获取项目的根目录因为tomcat和weblogic获取的根目录不一致,所以需要此方法
//            if(savePath == null){
//                savePath = this.getClass().getClassLoader().getResource("/").getPath();
//            }
//            String path = savePath+ File.separator +"mxuploadpath" + File.separator  + "T_GQJ_TESTINFO" + 
//                    File.separator + str[0] + File.separator + "attachment" + File.separator + fileName;
//    
//            // 以流的形式下载文件。
//            InputStream fis = new BufferedInputStream(new FileInputStream(path));
//            
//            // 设置response的Header
//            response.addHeader("Content-Disposition",
//                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
//            response.setContentType("application/x-download; charset=GBK");
//            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//            int len = 0;
//            byte[] buffer = new byte[1048576];
//            while ((len = fis.read(buffer, 0, buffer.length)) != -1) {
//                toClient.write(buffer, 0, len);
//            }
//            try {
//                fis.close();
//            } catch (Exception e1) {
//                e1.printStackTrace();
//            }
//            try {
//                toClient.flush();
//                toClient.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//    
//        }
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    /**
     * 图片下载
     */
    @RequestMapping("/getTestinfoPhoto")
    public @VoidResponseBody void exportOutUserInfo(HttpServletResponse response, HttpServletRequest request)
            throws IOException {
            //接收前台传来的参数
              String paths = request.getParameter("paths");
              //参数格式是文件地址+;;+文件名称
              //将参数拆分成地址+文件名
              String str[] = paths.split(";;");
              String filepath="";
              String fileName="";
              //将文件名拆分出来
              String photoName[] = str[1].split(";");

              Map<String, InputStream> isMap = new HashMap();
              String savePath = request.getSession().getServletContext().getRealPath("/");
            //获取项目的根目录因为tomcat和weblogic获取的根目录不一致,所以需要此方法
            if(savePath == null){
                savePath = this.getClass().getClassLoader().getResource("/").getPath();
            }
            //遍历将文件流和文件名放入map
              for(int i = 0; i<photoName.length ;i++){
                  filepath = str[0];//文件地址
                  fileName = photoName[i];//文件名
                  String path = savePath+ File.separator +"mxuploadpath" + File.separator  + "T_GQJ_TESTINFO" + 
                        File.separator + str[0] + File.separator + "attachment" + File.separator + fileName;
                  InputStream fis = new BufferedInputStream(new FileInputStream(path));
                  
                  isMap.put(fileName, fis);
              }
          response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=Document.zip");
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        
          //遍历map打包
          for (String key : isMap.keySet()){
              addToZip(isMap.get(key), zipOut, key);
          }
        zipOut.flush();
        zipOut.close();
    }
    /*添加压缩文件*/
    private void addToZip(InputStream is, ZipOutputStream zipOut, String fileName) {
        try{
            ZipEntry entry = new ZipEntry(fileName);
            zipOut.putNextEntry(entry);
            int len;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

-------------------------------------------------前端-------------------------------------------------

window.open(gqjtest.mappath("~/rest/tgqjtestinfo/getTestinfoPhoto?paths="+paths+""),"_parent");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值