SpringBoot下载压缩在zip文件(十四)

package com.song.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author song
 * @date 2020/7/4
 */
public class Zip {
    public static void main(String[] args) {
        List<File> files= new ArrayList<File>();
        File file=new File("E:\\upload");
        File[] files1 = file.listFiles();
        for (File file1 : files1) {
            files.add(file1);
        }

        File file1=new File("E:\\aaa\\dd.zip");
        Zip.zipUtil(files,file1);
    }
    /**
     *
     * @param fileName 文件名称
     * @param filePath 文件下载路径
     */
    public static void zipUtil(List<File> fileName,File filePath) {


        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            if (fileName != null && fileName.size() != 0) {

                fos = new FileOutputStream(filePath);
                zos = new ZipOutputStream(new BufferedOutputStream(fos));

                byte[] bytes = new byte[1024 * 10];
                for (File file : fileName) {
                    //压缩的文件名称
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zos.putNextEntry(zipEntry);
                    //读取压缩文件并写入压缩包
                    fis = new FileInputStream(file.getPath());

                    bis = new BufferedInputStream(fis, 1024 * 10);
                    int read = 0;
                    while ((read = bis.read(bytes, 0, 1024 * 10)) != -1) {
                        zos.write(bytes, 0, read);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
            // TODO: handle exception
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (zos != null) {
                    zos.flush();
                    zos.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
                // TODO: handle exception

            }
        }
    }
}

文件下载

package com.song.controller;

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author gssong
 * @description
 * @date 2020-9-23
 */
@RestController
public class DownloadController {
     @RequestMapping("/download1")
     public void download1(){
         InputStream is=null;
         OutputStream os=null;
         BufferedInputStream bs=null;
         File file=new File("C:\\Users\\Ivan\\Desktop\\es.docx");

         try {
             is=new FileInputStream(file);
             bs=new BufferedInputStream(is);
             os=new FileOutputStream("C:\\Users\\Ivan\\Desktop\\666.docx");
             byte[] bytes = new byte[1024 * 10];
             int read=0;
             while ((read=bs.read(bytes,0,1024*10))!=-1){
                 os.write(bytes,0,read);
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }finally {

             try {
                 is.close();
                 os.close();
                 bs.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

     }

    @RequestMapping("/download2")
    public void download2( HttpServletRequest req, HttpServletResponse resp){
        InputStream is=null;
        BufferedInputStream bs=null;
        File file=new File("C:\\Users\\Ivan\\Desktop\\es.docx");
        String filename_UTF8 = file.getName();

        try {
            is=new FileInputStream(file);
            bs=new BufferedInputStream(is);
            String userAgent = req.getHeader("user-agent");
            boolean isFirefox = userAgent!=null&&userAgent.contains("Firefox") ? true : false;
            String filename = isFirefox ? MimeUtility.encodeText(filename_UTF8, "UTF8", "B") : URLEncoder.encode(filename_UTF8, "UTF-8");
            resp.setHeader("Content-disposition", "attachment;filename=" +filename);

            byte[] bytes = new byte[1024 * 10];
            int read=0;
            ServletOutputStream outputStream = resp.getOutputStream();
            while ((read=bs.read(bytes,0,1024*10))!=-1){
                outputStream.write(bytes,0,read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                is.close();
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    @RequestMapping("/download3")
    public void download3( HttpServletRequest req, HttpServletResponse resp) throws IOException {
        OutputStream out= resp.getOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        String fileName = "test" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String agent = req.getHeader("User-Agent").toUpperCase();
        if (agent.indexOf("MSIE") > 0|| ( null != agent && -1 != agent.indexOf(") LIKE GECKO"))) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
            fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
        }
        String disposition = "attachment;filename=" +fileName+".zip";
        resp.setHeader("Content-disposition", disposition);
        List<File> files= new ArrayList<File>();


        File file=new File("C:\\Users\\Ivan\\Desktop\\test");
        File[] listFiles = file.listFiles();
        for (File f : listFiles) {
            files.add(f);
        }
        for (File fileInfo : files) {
            try {
                InputStream inpf = new FileInputStream(fileInfo);

                ZipEntry entry = new ZipEntry(fileInfo.getName());
                zipOutputStream.putNextEntry(entry);

                byte[] buffer = new byte[1024*4];
                int n = 0;
                while ((n = inpf.read(buffer,0,1024*10))!=-1) {
                    zipOutputStream.write(buffer, 0,n);
                }
                inpf.close();
                zipOutputStream.closeEntry();
            }catch (Exception e) {
                // TODO: handle exception
            }
        }



        zipOutputStream.flush();
        zipOutputStream.close();
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值