SpringMvc下载并压缩文件

浏览器通过file://访问文件和http://访问文件的区别

file协议用于访问本地计算机中的文件,好比通过资源管理器中打开文件一样,需要主要的是它是针对本地的,即file协议是访问你本机的文件资源。

http访问本地的html文件,相当于将本机作为了一台http服务器,然后通过localhost访问的是你自己电脑上的本地服务器,再通过http服务器去访问你本机的文件资源。

再简单点就是file只是简单请求了本地文件,将其作为一个服务器未解析的静态文件打开。而http是在本地搭建了一个服务器再通过服务器去动态解析拿到文件。

后端代码

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RestController
public class FileController {

    /**
     * 文件下载接口
     * @param response
     * @return
     */
    @GetMapping("/downloadFile")
    public Object downloadFile(HttpServletResponse response){
        try {
            OutputStream outputStream = response.getOutputStream();
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition",
                    "attachment; filename=aa.zip");

            List list = new ArrayList<>();

            Map file1 = new HashMap() {{
                put("fileName", "file1.jpg");
                put("url", "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1570090404,2634516241&fm=26&gp=0.jpg");
            }};

            Map file2 = new HashMap() {{
                put("fileName", "file2.jpg");
                put("url", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2668456178,697608865&fm=15&gp=0.jpg");
            }};
            list.add(file1);
            list.add(file2);
            toZip(list, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    /**
     * 压缩文件
     * @param files
     * @param out
     */
    public void toZip(List files, OutputStream out) {
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            for (Map file : files) {
                URL url = new URL(file.get("url"));
                HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
                httpUrl.connect();
                BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
                zos.putNextEntry(new ZipEntry(file.get("fileName")));
                int len = 2048;
                byte[] b = new byte[len];
                while ((len = bis.read(b)) != -1) {
                    zos.write(b, 0, len);
                }
                out.flush();
                bis.close();
                httpUrl.disconnect();
                zos.closeEntry();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

访问接口

image.png

打开压缩包,查看文件

image.png

你可能感兴趣的:(SpringMvc下载并压缩文件)