一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java 文件流的處理方式 文件打包成zip

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

2022-02-22 13:15介寒食 Java教程

這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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

1、下載文件到本地

?
1
2
3
4
5
6
7
8
9
10
11
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后端下載

方式一:

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

方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void zipFilesAll() throws Exception {
        String zipPath = "";//zip包路徑
        String zipFileName = "";//zip包名稱
        File zipFile = new File(zipFileName .toString());
 
        // 創(chuàng)建 FileOutputStream 對(duì)象
        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();
            }
 
            // 實(shí)例化 FileOutputStream 對(duì)象
            fileOutputStream = new FileOutputStream(zipFileName.toString());
            // 實(shí)例化 ZipOutputStream 對(duì)象
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            // 創(chuàng)建 ZipEntry 對(duì)象
            ZipEntry zipEntry = null;
            for (CL cl: ClList) {
                // 實(shí)例化 ZipEntry 對(duì)象,源文件數(shù)組中的當(dāng)前文件
                zipEntry = new ZipEntry(tCltjjl.getcClmc() + ".zip");
                zipOutputStream.putNextEntry(zipEntry);
                IOUtils.copy(new FileInputStream(cl.getcPath(), zipOutputStream);
            }
        } catch (Exception e) {
             
        }finally{
             //記得刪除文件
        }
    }

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--二維碼相關(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-->

前臺(tái)代碼:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script th:inline="javascript">
    function downloadzip(){
 
            var storeType = $("#storeType").val();
            if(storeType ==""){
                bootAlertError("請(qǐng)選擇門店!");
                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>

后臺(tái)代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
     * @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("----------下載文件名編碼時(shí)出現(xiàn)錯(cuò)誤------"+e.getMessage());
        }
        OutputStream outputStream = response.getOutputStream();
        ZipArchiveOutputStream zous = new ZipArchiveOutputStream(outputStream);
        zous.setUseZip64(Zip64Mode.AsNeeded);
        zous.setEncoding("utf-8");
        try{
            //我這里是通過不同產(chǎn)品類型生成不同產(chǎn)品的二維碼圖片流
            //具體你想生成什么類型的多個(gè)文件打包,只需要循環(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";
                //打包文件里的每個(gè)文件的名字
                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("---------------門店總代生成二維碼打包流出錯(cuò)----------------"+e.getMessage());
        }finally{
            if(outputStream != null){
                outputStream.close();
            }
            if(zous != null){
                zous.close();
            }
        }
    }

最后效果:

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

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/jiehanshi/p/11533465.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: blackedhd 18sex| 国产精品怡红院在线观看 | 欧美一级xxx| 精品卡1卡2卡三卡免费网站 | 色久激情 | 免费看60分钟大片视频播放 | 久久99精品久久久久久园产越南 | 四虎精品成人a在线观看 | 女人麻豆国产香蕉久久精品 | 国产成人精品系列在线观看 | 亚洲精品国产成人99久久 | 思思91精品国产综合在线 | 天美麻豆 | 奇米影视在线视频8888 | 韩国黄色片网站 | 免费观看欧美性一级 | 久久人妻少妇嫩草AV无码 | 欧美一级精品 | 亚洲上最大成网人站4438 | zoz.zzz色| 国产hd老太婆 | 91理论片午午伦夜理片久久 | 日韩精品视频在线播放 | 我与岳乱短篇小说 | 关晓彤被调教出奶水的视频 | 男男按摩1069gⅴ | 成人免费一区二区三区在线观看 | 激情乱文| 黑人破中国女人处 | 青青操在线观看 | np高h疯狂黄暴宫口 narutomanga玖辛奈之乳 | 描写细腻的高h肉 | 草莓视频丝瓜 | 日本福利片国产午夜久久 | 日本欧美不卡一区二区三区在线 | 车上小婕子系列辣文小说 | 亚洲日日做天天做日日谢 | 午夜国产精品福利在线观看 | 日本免费全黄一级裸片视频 | 欧美精品99久久久久久人 | 免费一级欧美片在线观免看 |