java導出excel並且壓縮成zip上傳到oss,並下載,使用字節流去存儲,不用文件流保存文件到本地


最近項目上要求實現導出excel並根據條數做分割,然后將分割后的多個excel打包成壓縮包上傳到oss服務器上,然后提供下載方法,具體代碼如下:這里只展示部分代碼,獲取數據的代碼就不展示了

ByteArrayOutputStream zipos = new ByteArrayOutputStream(61858764);//設置大小為60M
        ZipOutputStream zos = new ZipOutputStream (zipos) ;//創建壓縮流,初始化一個輸出流緩存區
        for(Entry<String, JSONObject> entry : mapData.entrySet()){
            String key = entry.getKey();
            JSONObject jsonVal = entry.getValue();
            OrderMonitorExcelViewSvc orderMonitorExcelView = new OrderMonitorExcelViewSvc(jsonVal);
            try {
                orderMonitorExcelView.outputToFile("orderMonitorExport_"+key);
            } catch (Exception e1) {
                logger.info(e1);
            }
            HSSFWorkbook workbook = orderMonitorExcelView.getWorkbook();
            ByteArrayOutputStream os = new ByteArrayOutputStream(61858764);//設置大小為60M
            try {
                workbook.write(os);
                      //創建一個壓縮文件里的名稱
                ZipEntry zipEntry = new ZipEntry("訂單監控查詢"+key+".xls");
                System.out.println(os.size());
                zipEntry.setSize(os.size());
                zipEntry.setCompressedSize(os.size());
                  zos.putNextEntry(zipEntry);
                  os.writeTo(zos);
                zos.closeEntry();      
                os.close();
            } catch (IOException e) {
                logger.info("寫入ZipOutputStream異常");
            }
        }
        try {
            zos.close();//注意關閉流的順序,在上傳oss之前必須關閉流否則下載解壓的時候會報“文件末端錯誤”的問題
            zipos.close();
            ByteArrayInputStream zipis = new ByteArrayInputStream(zipos.toByteArray());
            @SuppressWarnings("deprecation")
            int rel = cpsdCoralStorage.uploadFile("order_monitor_data/"+batchno,zipis);
            if(rel==0){
                orderMonitorDao.updateDownloadBatch("0", batchno);
            }
            zipis.close();
        } catch (IOException e) {
            logger.info("上傳oss失敗");
            logger.info("關閉壓縮流異常"+e);
        }

下載方法:

@RequestMapping(value = { "downloadExport" })
    public void downloadExport(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "batchno", required = true) String batchno) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formatDate = sdf.format(new Date());
        String pathName = "order_monitor_data/" + batchno;
        String srcFileName = "導出結果" + formatDate + ".zip";
        InputStream is = cpsdCoralStorage.downloadFile(pathName);
        try {
            response.setHeader("content-disposition",
                "attachment;filename=" + URLEncoder.encode(srcFileName, "utf-8"));
            OutputStream out = response.getOutputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int temp =0;
            while((temp=is.read())!=-1){ // 讀取內容  
                baos.write(temp);
            }
            byte[] xlsBytes = baos.toByteArray();
            out.write(xlsBytes);
            out.flush();
            out.close();
        }
        catch (FileNotFoundException e) {
            logger.logException(e);
        }
        catch (IOException e) {
            logger.logException(e);
        }
    }

本次分享主要是因為之前導出都是先存一個文件,需要找一個存儲目錄,這樣發布到生產環境會有各種問題,還需要看文件夾是否有讀寫權限,我用的這個方法是直接設置一個存儲流,直接對流進行操作,省去了這些麻煩!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM