亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java 文件流的處理方式 文件打包成zip

 更新時間:2021年10月19日 09:52:50   作者:介寒食  
這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

java 文件流的處理 文件打包成zip

1、下載文件到本地

public void download(HttpServletResponse response){
    String filePath ="";//文件路徑
    String fileName ="";//文件名稱
    // 讀到流中
    InputStream inStream = new FileInputStream(filePath);
    // 設(shè)置輸出的格式
    response.reset();
     response.setContentType("bin");
     response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
     IOUtils.copy(inStream, response.getOutputStream());
}

2、java后端下載

方式一:

new URL(fileUrl + item.getcBhFileserver()).openStream()

方法二:

    public Boolean addFile(String url, String id, String fileName) { 
        RequestCallback requestCallBack = new RequestCallback() {
 
            @Override
            public void doWithRequest(ClientHttpRequest request) throws IOException {
                request.getHeaders().add("accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            }
        };
 
        ResponseExtractor<Boolean> responseExtractor = new ResponseExtractor<Boolean>() { 
            @Override
            public Boolean extractData(ClientHttpResponse response) throws IOException {
                if (response.getStatusCode() == HttpStatus.OK) {
                    //得到文件流
                    InputStream input = response.getBody();
                    return true;
                }
                return false;
            }
        };
        return restTemplate.execute(url, HttpMethod.GET, requestCallBack, responseExtractor, id);
    }

3、文件打包成zip

public void zipFilesAll() throws Exception {
        String zipPath = "";//zip包路徑
        String zipFileName = "";//zip包名稱
        File zipFile = new File(zipFileName .toString());
 
        // 創(chuàng)建 FileOutputStream 對象
        FileOutputStream fileOutputStream = null;
        // 創(chuàng)建 ZipOutputStream
        ZipOutputStream zipOutputStream = null;
        try {
            //創(chuàng)建文件夾
            zipFile = new File(zipPath );
            FileUtils.forceMkdir(zipFile);
 
            //創(chuàng)建文件
            zipFile = new File(zipFileName .toString());
            if (!zipFile.exists()) {
                zipFile.createNewFile();
            }
 
            // 實例化 FileOutputStream 對象
            fileOutputStream = new FileOutputStream(zipFileName.toString());
            // 實例化 ZipOutputStream 對象
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            // 創(chuàng)建 ZipEntry 對象
            ZipEntry zipEntry = null;
            for (CL cl: ClList) {
                // 實例化 ZipEntry 對象,源文件數(shù)組中的當(dāng)前文件
                zipEntry = new ZipEntry(tCltjjl.getcClmc() + ".zip");
                zipOutputStream.putNextEntry(zipEntry);
                IOUtils.copy(new FileInputStream(cl.getcPath(), zipOutputStream);
            }
        } catch (Exception e) {
             
        }finally{
             //記得刪除文件
        }
    }   

后臺多文件打包成zip返回流 前臺提供按鈕一鍵下載

項目pom文件添加二維碼操作,和文件打包的maven支持:

        <!--二維碼相關(guān) start-->
        <dependency>
            <groupId>net.glxn.qrgen</groupId>
            <artifactId>javase</artifactId>
            <version>2.0</version>
        </dependency>
        <!--二維碼相關(guān) end-->
 
        <!--文件打包相關(guān) start-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.12</version>
        </dependency>
        <!--文件打包相關(guān) end-->

前臺代碼:

<button type="button" onclick="downloadzip()">下載</button>

js(我用了thymeleaf模板)代碼:

<script th:inline="javascript">
    function downloadzip(){
 
            var storeType = $("#storeType").val();
            if(storeType ==""){
                bootAlertError("請選擇門店!");
                return;
            }
            var url = [[@{/downLoadProductQrCode/getStreamZip}]];
 
            //模擬form表單 返回打包流
            var form = $('<form method= "POST" action="'+ url +'" enctyped="multipart/form-data">'+
                        '<input name= "agencyId" type= "hidden" value="'+storeType+'" />'
                        +'</form>');
            form. appendTo('body'). submit(). remove();
        }
</script>

后臺代碼:

/**
     * @Author Ni Klaus
     * @Description //TODO 門店總代生成打包產(chǎn)品二維碼zip
     * @Date 上午 10:38 2019/8/20 0020
     * @Param [params,response]
     * @return void
     **/
    @RequestMapping({"getStreamZip"})
    @ResponseBody
    public void findList(@RequestParam Map params,HttpServletResponse response) throws Exception {
        String agencyId = (String) params.get("agencyId");
        AgencyAccount agencyAccount = agencyAccountService.getAccountByAgencyId(agencyId);
        //這里設(shè)置打包后的zip文件名
        String downloadName = agencyAccount.getName()+".zip";
        try{
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + new String(downloadName.getBytes(),"ISO8859-1"));
        }catch(UnsupportedEncodingException e){
            log.error("----------下載文件名編碼時出現(xiàn)錯誤------"+e.getMessage());
        }
        OutputStream outputStream = response.getOutputStream();
        ZipArchiveOutputStream zous = new ZipArchiveOutputStream(outputStream);
        zous.setUseZip64(Zip64Mode.AsNeeded);
        zous.setEncoding("utf-8");
        try{
            //我這里是通過不同產(chǎn)品類型生成不同產(chǎn)品的二維碼圖片流
            //具體你想生成什么類型的多個文件打包,只需要循環(huán)創(chuàng)建ArchiveEntry 然后zous.putArchiveEntry(entry)就可以了
            StoreProductType[] storeProductTypes = StoreProductType.values();
            for (StoreProductType storeProductType : storeProductTypes) {
                String url = "http://m.xxx.cn/goods/pay/xxx.html?productid="+storeProductType.getProductId()
                        + "&agencycode="+ agencyId +"&channelid=def&appfrom=sqjk&isstore=1";
                //打包文件里的每個文件的名字
                String imgName = storeProductType.getDescription()+".png";
                ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG).withSize(300,300).stream();
                byte[] bytes = out.toByteArray();
                ArchiveEntry entry = new ZipArchiveEntry(imgName);
                zous.putArchiveEntry(entry);
                zous.write(bytes);
                zous.closeArchiveEntry();
                if (out != null) {
                    out.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("---------------門店總代生成二維碼打包流出錯----------------"+e.getMessage());
        }finally{
            if(outputStream != null){
                outputStream.close();
            }
            if(zous != null){
                zous.close();
            }
        }
    }

最后效果:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot:Idea從零開始初始化后臺項目的教程

    Spring?Boot:Idea從零開始初始化后臺項目的教程

    這篇文章主要介紹了Spring?Boot:Idea從零開始初始化后臺項目的教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot2.0整合Redis自定義注入bean組件配置的實戰(zhàn)教程

    SpringBoot2.0整合Redis自定義注入bean組件配置的實戰(zhàn)教程

    這篇文章主要介紹了SpringBoot2.0整合Redis自定義注入bean組件配置,我們將基于SpringBoot2.0整合搭建的微服務(wù)項目為奠基,開啟中間件Redis的實戰(zhàn)之路,需要的朋友可以參考下
    2023-06-06
  • 詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式

    詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式

    這篇文章主要介紹了詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java父子線程之間實現(xiàn)共享傳遞數(shù)據(jù)

    java父子線程之間實現(xiàn)共享傳遞數(shù)據(jù)

    本文介紹了Java中父子線程間共享傳遞數(shù)據(jù)的幾種方法,包括ThreadLocal變量、并發(fā)集合和內(nèi)存隊列或消息隊列,并提醒注意并發(fā)安全問題
    2025-02-02
  • idea搭建mybatis環(huán)境配置全過程

    idea搭建mybatis環(huán)境配置全過程

    本文介紹了如何以IDEA搭建MyBatis環(huán)境配置的方法,包括步驟和注意事項,通過本文的介紹,可以輕松地以IDEA搭建MyBatis環(huán)境配置,提高開發(fā)效率
    2023-10-10
  • Java排序算法之歸并排序簡單實現(xiàn)

    Java排序算法之歸并排序簡單實現(xiàn)

    這篇文章主要介紹了Java排序算法之歸并排序簡單實現(xiàn),具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java執(zhí)行JavaScript代碼

    Java執(zhí)行JavaScript代碼

    這篇文章主要為大家詳細介紹了Java執(zhí)行JavaScript代碼的具體操作方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Java 實戰(zhàn)項目之疫情人員流動管理系統(tǒng)詳解

    Java 實戰(zhàn)項目之疫情人員流動管理系統(tǒng)詳解

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個疫情人員流動管理系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • mybatis中的if-test判斷解讀

    mybatis中的if-test判斷解讀

    在使用MyBatis進行條件判斷時,如果條件中涉及到字符與數(shù)字的比較,需要特別注意比較方式,例如,在<if>標(biāo)簽中,比較數(shù)字“1”時,應(yīng)將其寫在雙引號中,或者使用.toString()方法,避免直接使用字符'1'進行比較
    2024-11-11
  • java 二叉查找樹實例代碼

    java 二叉查找樹實例代碼

    這篇文章主要介紹了java 二叉查找樹實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評論