最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java如何把文件夾打成壓縮包并導(dǎo)出

 更新時間:2022年01月29日 11:45:41   作者:王紹樺  
這篇文章主要介紹了Java如何把文件夾打成壓縮包并導(dǎo)出,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

把文件夾打成壓縮包并導(dǎo)出

1.打壓縮包業(yè)務(wù)類

@Controller
public class AdminController {
    private String filePath = AdminController.class.getResource("/").getPath().split("WEB-INF")[0]+ "upload/";    
    @RequestMapping(value = "export_zip.htm", method = {RequestMethod.GET, RequestMethod.POST })
    public void zipwordDownAction(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //打包文件的存放路徑                     
        ZipCompressorByAnt zc = new  ZipCompressorByAnt(filePath+ "/file.zip");
        //需要打包的文件路徑
        zc.compress(filePath+ "/file/");
        String contentType = "application/octet-stream";
        try {
            //導(dǎo)出壓縮包
            download(request, response, "upload/file.zip", contentType,encodeChineseDownloadFileName(request, "file.zip"));
        } catch (Exception e) {
            request.getSession().setAttribute("msg", "暫無內(nèi)容");
        }
        //如果原壓縮包存在,則刪除
        File file=new File(filePath+ "/file.zip");
        if(file.exists()){
            file.delete(); 
        }
    }   
    /** 
     * 下載文件  
     */  
    public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String ctxPath =FileUtil.class.getResource("/").getPath().split("WEB-INF")[0];
        String downLoadPath = ctxPath + storeName;
        long fileLength = new File(downLoadPath).length();
        response.setContentType(contentType);
        response.setHeader("Content-disposition", "attachment; filename="
                + new String(realName.getBytes("utf-8"), "ISO8859-1"));
        response.setHeader("Content-Length", String.valueOf(fileLength));
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }
        bis.close();
        bos.close();
    }
    /** 
     * 對文件流輸出下載的中文文件名進(jìn)行編碼 屏蔽各種瀏覽器版本的差異性  
     */  
    public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) throws UnsupportedEncodingException {  
         String filename = null;    
            String agent = request.getHeader("USER-AGENT");    
            if (null != agent){    
                if (-1 != agent.indexOf("Firefox")) {//Firefox    
                    filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8"))))+ "?=";    
                }else if (-1 != agent.indexOf("Chrome")) {//Chrome    
                    filename = new String(pFileName.getBytes(), "ISO8859-1");    
                } else {//IE7+    
                    filename = java.net.URLEncoder.encode(pFileName, "UTF-8");    
                    filename = StringUtils.replace(filename, "+", "%20");//替換空格    
                }    
            } else {    
                filename = pFileName;    
            }    
            return filename;   
    }  

2.調(diào)用工具類

import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
public class ZipCompressorByAnt {
    private File zipFile;  
    public ZipCompressorByAnt(String pathName) {  
        zipFile = new File(pathName);  
    }  
    public void compress(String srcPathName) {  
        File srcdir = new File(srcPathName);  
        if (!srcdir.exists())  
            throw new RuntimeException(srcPathName + "不存在!");  
        Project prj = new Project();  
        Zip zip = new Zip();      
        zip.setProject(prj);  
        zip.setDestFile(zipFile);  
        FileSet fileSet = new FileSet();  
        fileSet.setProject(prj);   
        fileSet.setDir(srcdir);  
        //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setIncludes("*.java");  
        //fileSet.setExcludes(...); 排除哪些文件或文件夾  
        zip.addFileset(fileSet);            
        zip.execute();  
    }  
}

生成zip文件并導(dǎo)出

總結(jié)一下

關(guān)于Java下載zip文件并導(dǎo)出的方法,瀏覽器導(dǎo)出。

String downloadName = "下載文件名稱.zip";
? ? ? ? downloadName = BrowserCharCodeUtils.browserCharCodeFun(request, downloadName);//下載文件名亂碼問題解決
? ? ? ??
? ? ? ? //將文件進(jìn)行打包下載
? ? ? ? try {
? ? ? ? ? ? OutputStream out = response.getOutputStream();
? ? ? ? ? ? byte[] data = createZip("/fileStorage/download");//服務(wù)器存儲地址
? ? ? ? ? ? response.reset();
? ? ? ? ? ? response.setHeader("Content-Disposition","attachment;fileName="+downloadName);
? ? ? ? ? ? response.addHeader("Content-Length", ""+data.length);
? ? ? ? ? ? response.setContentType("application/octet-stream;charset=UTF-8");
? ? ? ? ? ? IOUtils.write(data, out);
? ? ? ? ? ? out.flush();
? ? ? ? ? ? out.close();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }

獲取下載zip文件流

public byte[] createZip(String srcSource) throws Exception{
? ? ? ? ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
? ? ? ? ZipOutputStream zip = new ZipOutputStream(outputStream);
? ? ? ? //將目標(biāo)文件打包成zip導(dǎo)出
? ? ? ? File file = new File(srcSource);?
? ? ? ? a(zip,file,"");
? ? ? ? IOUtils.closeQuietly(zip);
? ? ? ? return outputStream.toByteArray();
? ? }
public void a(ZipOutputStream zip, File file, String dir) throws Exception {
? ? ? ? ? ? //如果當(dāng)前的是文件夾,則進(jìn)行進(jìn)一步處理
? ? ? ? ? ? if (file.isDirectory()) {
? ? ? ? ? ? ? ? //得到文件列表信息
? ? ? ? ? ? ? ? File[] files = file.listFiles();
? ? ? ? ? ? ? ? //將文件夾添加到下一級打包目錄
? ? ? ? ? ? ? ? zip.putNextEntry(new ZipEntry(dir + "/"));
? ? ? ? ? ? ? ? dir = dir.length() == 0 ? "" : dir + "/";
? ? ? ? ? ? ? ? //循環(huán)將文件夾中的文件打包
? ? ? ? ? ? ? ? for (int i = 0; i < files.length; i++) {
? ? ? ? ? ? ? ? ? ? a(zip, files[i], dir + files[i].getName()); ? ? ? ? //遞歸處理
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else { ? //當(dāng)前的是文件,打包處理
? ? ? ? ? ? ? ? //文件輸入流
? ? ? ? ? ? ? ?BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
? ? ? ? ? ? ? ?ZipEntry entry = new ZipEntry(dir);
? ? ? ? ? ? ? ?zip.putNextEntry(entry);
? ? ? ? ? ? ? ?zip.write(FileUtils.readFileToByteArray(file));
? ? ? ? ? ? ? ?IOUtils.closeQuietly(bis);
? ? ? ? ? ? ? ?zip.flush();
? ? ? ? ? ? ? ?zip.closeEntry();
? ? ? ? ? ? }
? ? }

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

相關(guān)文章

  • SpringBoot與SpringCache概念用法大全

    SpringBoot與SpringCache概念用法大全

    這篇文章主要介紹了SpringBoot與SpringCache的概念及基本用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Java如何實(shí)現(xiàn)自定義異常類

    Java如何實(shí)現(xiàn)自定義異常類

    這篇文章主要介紹了Java如何實(shí)現(xiàn)自定義異常類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot如何加載多個YAML配置文件

    SpringBoot如何加載多個YAML配置文件

    在Spring?Boot中加載多個?YAML?配置文件是一個常見的需求,通常用于將配置信息分離到多個文件中以便于管理和維護(hù),下面就跟隨小編一起來看看具體實(shí)現(xiàn)吧
    2025-02-02
  • 詳解Spring Boot實(shí)戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

    詳解Spring Boot實(shí)戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作

    本篇文章主要介紹了Spring Boot實(shí)戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Java 超詳細(xì)講解抽象類與接口的使用

    Java 超詳細(xì)講解抽象類與接口的使用

    對于面向?qū)ο缶幊虂碚f,抽象是它的一大特征之一,在 Java 中可以通過兩種形式來體現(xiàn)OOP的抽象:接口和抽象類,下面這篇文章主要給大家介紹了關(guān)于Java入門基礎(chǔ)之抽象類與接口的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解

    解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解

    本篇文章是對Linux系統(tǒng)中JVM內(nèi)存2GB上限進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)

    MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)

    這篇文章主要介紹了MyBatis-Plus 查詢指定字段的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例

    Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例

    這篇文章主要介紹了Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式

    SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式

    這篇文章主要介紹了SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring使用RestTemplate和Junit單元測試的注意事項(xiàng)

    Spring使用RestTemplate和Junit單元測試的注意事項(xiàng)

    這篇文章主要介紹了Spring使用RestTemplate和Junit單元測試的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論

格尔木市| 西藏| 任丘市| 沂水县| 万盛区| 达拉特旗| 奈曼旗| 南宫市| 辛集市| 正宁县| 改则县| 莱西市| 安陆市| 南昌市| 安溪县| 英吉沙县| 双江| 泰和县| 盐山县| 磐安县| 涞源县| 乌兰县| 建德市| 巴中市| 通化县| 梅州市| 天津市| 乌苏市| 邢台市| 航空| 临海市| 宁晋县| 深圳市| 石台县| 湛江市| 正定县| 宜良县| 普宁市| 潢川县| 禹州市| 新蔡县|