欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

把文件夾打成壓縮包并導(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();
    }
    /** 
     * 對(duì)文件流輸出下載的中文文件名進(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ù)器存儲(chǔ)地址
? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? //將文件夾添加到下一級(jí)打包目錄
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? }
? ? }

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

相關(guān)文章

  • springboot日期格式化及時(shí)差問題分析

    springboot日期格式化及時(shí)差問題分析

    這篇文章主要介紹了springboot日期格式化,時(shí)差問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • 解決springboot responseentity<string>亂碼問題

    解決springboot responseentity<string>亂碼問題

    這篇文章主要介紹了解決springboot responseentity<string>亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 設(shè)計(jì)模式之責(zé)任鏈模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    設(shè)計(jì)模式之責(zé)任鏈模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了設(shè)計(jì)模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 新版POI獲取日期類型cell值過程圖解

    新版POI獲取日期類型cell值過程圖解

    這篇文章主要介紹了新版POI獲取日期類型cell值過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java模擬撲克牌洗牌實(shí)現(xiàn)生成52張撲克的方法示例

    Java模擬撲克牌洗牌實(shí)現(xiàn)生成52張撲克的方法示例

    這篇文章主要介紹了Java模擬撲克牌洗牌實(shí)現(xiàn)生成52張撲克的方法,涉及Java數(shù)組遍歷、重排及輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • springboot結(jié)合mysql主從來實(shí)現(xiàn)讀寫分離的方法示例

    springboot結(jié)合mysql主從來實(shí)現(xiàn)讀寫分離的方法示例

    這篇文章主要介紹了springboot結(jié)合mysql主從來實(shí)現(xiàn)讀寫分離的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Spring Cloud Gateway層限流實(shí)現(xiàn)過程

    Spring Cloud Gateway層限流實(shí)現(xiàn)過程

    這篇文章主要介紹了Spring Cloud Gateway層限流實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java.lang.UnsupportedOperationException的問題解決

    java.lang.UnsupportedOperationException的問題解決

    本文主要介紹了java.lang.UnsupportedOperationException的問題解決,該錯(cuò)誤表示調(diào)用的方法不被支持或不可用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解

    java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • spring Roo安裝使用簡(jiǎn)介

    spring Roo安裝使用簡(jiǎn)介

    這篇文章主要介紹了spring Roo安裝使用簡(jiǎn)介,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12

最新評(píng)論