springMVC 指定文件 压缩下载

package com.lyt.springboot.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyt.springboot.dao.UserDao;
import com.lyt.springboot.entity.User;
import com.sun.deploy.net.HttpResponse;
import com.sun.deploy.net.URLEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.*;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-05-15 18:42:17
 */
@Controller
@RequestMapping("/download")
public class DownloadController {

    public static final String filepath = "D:\\桌面文件\\test\\";
    @RequestMapping(value = "/file",method = RequestMethod.GET)
    public void file(@RequestParam("filename") String filename, HttpServletResponse response) throws UnsupportedEncodingException {
//        String encode = URLEncoder.encode(filename, "UTF-8");
//        response.setContentType("application/zip;charset=UTF-8");
//        // 4. 附件下载 attachment 附件 inline 在线打开(默认值)
//        response.setHeader("content-disposition", "attachment;fileName=");

        //文件路径
        String fileNames = filepath + filename;
        //生成文件夹
//        File file = new File(fileName);
        //压缩成zip
        ZipUtil.zip(FileUtil.file(fileNames.concat(".zip")), false,
                FileUtil.file(fileNames.concat(".docx")),
                FileUtil.file(fileNames.concat(".txt"))
        );
        File zipFile = new File(fileNames.concat(".zip"));

        //下载
        try {
         /*   InputStream fis = new BufferedInputStream(new FileInputStream(zipFile));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();*/

            FileInputStream is = new FileInputStream(zipFile);
            // 3. 获取响应输出流
            response.setContentType("application/zip;charset=UTF-8");
            // 4. 附件下载 attachment 附件 inline 在线打开(默认值)
            response.setHeader("content-disposition", "attachment;fileName=" + filename.concat(".zip"));
            // 5. 处理下载流复制
            ServletOutputStream os = response.getOutputStream();
            int len;
            byte[] b = new byte[1024];
            while(true){
                len = is.read(b);
                if(len == -1) break;
                os.write(b, 0, len);
            }
            // 释放资源
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //删除文件 和 zip
            zipFile.delete();
        }



    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值