1. 程式人生 > >java實現阿里OSS物件儲存多檔案批量zip壓縮下載

java實現阿里OSS物件儲存多檔案批量zip壓縮下載

版權宣告

版權宣告:本文由 低調小熊貓 發表於 低調小熊貓的部落格 轉載宣告:自由轉載-非商用-非衍生-保持署名,非商業轉載請註明作者及出處,商業轉載請聯絡作者本人qq:2696284032 文章連結:https://aodeng.cc/archives/javaosszipdown

簡介

阿里oss物件儲存官方文件我看了,但是真實開發,還不能滿足需求,阿里檔案下載路徑只能寫死,下載檔案也只能單個下載,所以自己整理了一些程式碼,下載時可以自己選擇儲存的路徑,可以實現多個檔案同時下載,不吹逼了,反正很厲害就是了,話不多說,上程式碼

程式碼

<td>
<a class="btn btn-mini btn-danger" style="width: 88px;height:30px;margin-top: -25px" 
href="${pageContext.request.contextPath}/pproject/ossdownfile?projectId=${projectId}">
下載
</a>
</td>

3.功能實現的程式碼

/**
     * oss檔案下載
     */
    @RequestMapping("/ossdownfile")  
    public void ossdownfile(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="projectId",required=false) long projectId) throws Exception{ 
        try {
            //-----------------------低調小熊貓批量下載檔案------------------------//
            //獲取檔名稱
            String Name=pprojectService.findOne(projectId).getCheckFileName();
            //模擬檔案
            String fileName = request.getSession().getServletContext().getRealPath("tmp/check/")+"/"+projectId+"/"+Name;  
            // 建立臨時檔案
            File zipFile = File.createTempFile("temp", ".zip");
            FileOutputStream fps = new FileOutputStream(zipFile);
            CheckedOutputStream csum = new CheckedOutputStream(fps, new Adler32());//註釋一下Adler32(較快)和CRC32兩種
            // 用於將資料壓縮成Zip檔案格式
            ZipOutputStream zos = new ZipOutputStream(csum);
            //獲取檔案的name並拼接成key,下載檔案
            File f = new File(PathUtil.getRealPath()+"\\tmp\\files\\"+projectId);
            if(f.exists()){
                File[] fs = f.listFiles();
                if(null!=fs){
                    for(File file0:fs){
                        //善意的提醒一下,key不能以任何形式的斜槓開頭
                        InputStream inputStream = ossUtil.getObjectForInputStream("files/"+projectId+"/"+file0.getName()).getObjectContent();
                        zos.putNextEntry(new ZipEntry(file0.getName()));//確保壓縮包裡面檔案不同名
                        int bytesRead = 0;
                        // 向壓縮檔案中輸出資料
                        while((bytesRead=inputStream.read())!=-1){
                            zos.write(bytesRead);
                        }
                        inputStream.close();
                        zos.closeEntry();
                    }                    
                }
            }
            zos.close();
            //轉碼,免得檔名中文亂碼  
            Name = URLEncoder.encode(Name,"UTF-8");  
            //設定檔案下載頭  
            response.addHeader("Content-Disposition", "attachment;filename=" + Name);    
            //1.設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別    
            response.setContentType("application/octet-stream");
            //寫入檔案
            FileInputStream fis = new FileInputStream(zipFile);
            BufferedInputStream buff = new BufferedInputStream(fis);
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); 
            //通知瀏覽器以附件形式下載            
            response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(Name,"utf-8"));
            byte[] car=new byte[1024];
            int L=0;
            while (L < zipFile.length()) {
                int j = buff.read(car, 0, 1024);
                L += j;
                out.write(car, 0, j);
            }
            if(out!=null){
                out.flush();
                out.close();
            }
            ossUtil.clientShutdown();
            // 刪除檔案
            zipFile.delete();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }