自定义批量图片打包下载

   请求的方法:
    @RequestMapping("/batchDownload")
    public void batchCode(HttpServletRequest request, HttpServletResponse response, String tables) throws Exception {
        byte [] data = photoImgService.downloadItemImage(tables.split(","));
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"images.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
        IOUtils.closeQuietly(response.getOutputStream());
    }

/**
     *自定义批量下载图片实现 
     * @throws Exception 
     */
    @Override
    public byte[] downloadItemImage(String[] tableNames) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            String imagename0 = tableName.substring(tableName.lastIndexOf("/")+1);
            String imagename = imagename0.substring(0,imagename0.lastIndexOf("."));
            //将当前的图片放到zip流中传出去
            downLoadImages(tableName,imagename,zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
    /**
     * 自定义下载图片方法
     * 图片路径
     * 图片名称
     * @throws Exception 
     * 
     */
    public static void downLoadImages(String imagePath,String imageName,ZipOutputStream zip) throws Exception{
        String imgArray[] = {"jpg","png","gif","bmp","jpeg"};
        List<String> suffixList = Arrays.asList(imgArray);
        String suffix = imagePath.substring(imagePath.lastIndexOf(".") + 1);
        
        if(imagePath!=""&&!imagePath.equals(null)){
            BufferedInputStream in = null;    
            try {
                URL url = new URL(imagePath);  
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
                conn.setConnectTimeout(5 * 1000);  
                conn.setRequestMethod("GET");  
                conn.setRequestProperty(  
                    "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "  
                    + "application/x-shockwave-flash, application/xaml+xml, "  
                    + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "  
                    + "application/x-ms-application, application/vnd.ms-excel, "  
                    + "application/vnd.ms-powerpoint, application/msword, */*");  
                conn.setRequestProperty("Accept-Language", "zh-CN");  
                conn.setRequestProperty("Charset", "UTF-8");  
                InputStream inStream = conn.getInputStream(); 
                //校验读取到文件
                if (suffixList.contains(suffix)) {// 文件格式不对
                    imageName += "."+suffix;                  
                }else {
                    throw new Exception("不是图片");  
                }
                if(inStream == null) {
                    throw new Exception("获取压缩的数据项失败! 数据项名为:" + imageName);  
                }else {
                    in = new BufferedInputStream(inStream);
                }
                //校验读取到文件
                // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样  
                ZipEntry zipEntry = new ZipEntry("图片/"+imageName);
                // 定位到该压缩条目位置,开始写入文件到压缩包中  
                zip.putNextEntry(zipEntry);
                byte[] bytes = new byte[1024 * 5];// 读写缓冲区  
                int read = 0;  
                while ((read = in.read(bytes)) != -1) {
                    zip.write(bytes, 0, read);  
                }  
                IOUtils.closeQuietly(inStream);//关掉输入流
                IOUtils.closeQuietly(in);//关掉缓冲输入流
                zip.closeEntry();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

转载请说明出处:程序员花费了很长时间,才搞定的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值