java批量下載,將多檔案打包成zip格式下載

來源:互聯網
上載者:User

標籤:key   sed   mapping   cep   壓縮包   產品   mode   dpi   int   

現在的需求的:

根據產品族、產品類型,下載該產品族、產品類型下面的pic包;

pic包是zip壓縮檔;

t_product表:

這些包以blob形式存在另一張表中:

t_imagefile表:

現在要做的是:將接入網、OLT下面的兩個包downloadPIC:MA5800系列-pic.zip 和 MA5900-pic.rar一起打包成zip壓縮檔下載下來;

 

代碼:

ProductController.java:

    /**     * 根據產品族、產品類型下載照片包     */    @RequestMapping("/downloadwBatch")    public void downloadwBatch(HttpServletRequest request, HttpServletResponse response, String productFamily, String productType){        //http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網&productType=OLT        try {            productFamily = new String(productFamily.getBytes("iso-8859-1"), "utf-8");            productType = new String(productType.getBytes("iso-8859-1"), "utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }                //擷取要下載的照片包名        Map<String, String> params = new HashMap<String, String>();        params.put("productFamily", productFamily);        params.put("productType", productType);        List<String> packageNames = productService.getPackageNamesByFamilyAndType(params);                //根據包名擷取待下載的檔案    檔案名稱-位元組數組的形式        Map<String, byte[]> files = new HashMap<String, byte[]>();        for(String packageName : packageNames){            byte[] f = productService.getPackage(packageName);            if(f!=null){                files.put(packageName, f);            }        }                //設定下載的壓縮包名        String zipName = productFamily + "_"+ productType + ".zip";                //根據檔案,進行壓縮,批量下載        if(files.size() > 0){            productService.downloadBatchByFile(response, files, zipName);        }            }

ProductService.java:

    /**     * 根據包名擷取檔案     */    public byte[] getPackage(String packageName){        byte[] bag  = null;        try{            ImageFile m = productMapper.getPackage(packageName);            if(m!=null){                bag = m.getPicture();            }        }catch(Exception e){            e.printStackTrace();        }        return bag;    }            /**     * 根據產品族、產品類型 擷取待下載的包名     * @param params     * @return     */    public List<String> getPackageNamesByFamilyAndType(Map<String, String> params) {        List<String> packageNames = productMapper.getPackageNamesByFamilyAndType(params);                return packageNames;    }        /**     * 根據檔案,進行壓縮,批量下載     * @param response     * @param files     * @throws Exception      */    public void downloadBatchByFile(HttpServletResponse response, Map<String, byte[]> files, String zipName){        try{            response.setContentType("application/x-msdownload");            response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(zipName, "utf-8"));                        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());            BufferedOutputStream bos = new BufferedOutputStream(zos);                        for(Entry<String, byte[]> entry : files.entrySet()){                String fileName = entry.getKey();            //每個zip檔案名稱                byte[]    file = entry.getValue();            //這個zip檔案的位元組                                BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file));                zos.putNextEntry(new ZipEntry(fileName));                                int len = 0;                byte[] buf = new byte[10 * 1024];                while( (len=bis.read(buf, 0, buf.length)) != -1){                    bos.write(buf, 0, len);                }                bis.close();                bos.flush();            }            bos.close();        }catch(Exception e){            e.printStackTrace();        }    }

ProductMapper.java:

    /**     * 根據包名擷取檔案     */    public ImageFile getPackage(String packageName) throws Exception;    /**     * 據產品族、產品類型 擷取待下載的包名     */    public List<String> getPackageNamesByFamilyAndType(Map<String, String> params);
View Code

ProductMapper.xml:

    <!-- 根據包名擷取檔案 -->    <select id="getPackage" parameterType="java.lang.String" resultType="com.cy.model.ImageFile">        select * from t_imagefile where packageName = #{packageName}    </select>        <!--  跟據產品族、產品類型 擷取待下載的包名 -->    <select id="getPackageNamesByFamilyAndType" parameterType="java.util.Map" resultType="java.lang.String">        select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPic        where p.productFamily = #{productFamily} and p.productType = #{productType}    </select>
View Code

 

測試:

在瀏覽器中輸入:http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網&productType=OLT

下載結果如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---------

 

java批量下載,將多檔案打包成zip格式下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.