<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>io</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 8023
package com.sadhu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName IoApplication
* @Description TODO
* @Author sadhu
* @Date 2022/2/25 9:02
**/
@SpringBootApplication
public class IoApplication {
public static void main(String[] args) {
SpringApplication.run(IoApplication.class, args);
}
}
package com.sadhu.controller;
import cn.hutool.core.util.CharsetUtil;
import com.sadhu.service.ZipService;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.zip.ZipOutputStream;
/**
* @ClassName ZipController
* @Description TODO
* @Author sadhu
* @Date 2022/2/25 9:05
**/
@RestController
@RequestMapping("/file")
public class ZipController {
@Autowired
private ZipService zipService;
/**
* 下载zip包
*
* @param response
* @param fileName
*/
@RequestMapping(value = "/downLoadZipFile", method = RequestMethod.GET)
public void downLoadZipFile(HttpServletResponse response, @RequestParam("fileName") String fileName) throws IOException {
// 导出到浏览器的文件名
String zipFileName = fileName + ".zip";
// zip包输出流
ZipOutputStream zipOutputStream = null;
try {
// 清除buffer,清除首部的空白行
response.reset();
// 不同类型的文件对应不同的MIME类型
response.setContentType("application/x-zip-compressed");
// 设置编码格式
response.setCharacterEncoding("utf-8");
// 设置响应头
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, CharsetUtil.UTF_8));
// 创建zip包输出流,并设置编码格式
zipOutputStream = new ZipOutputStream(response.getOutputStream(), Charset.forName(CharsetUtil.UTF_8));
// 将文件流写入输出流
zipService.downLoadZipFile(zipOutputStream);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 完成编写ZIP输出流的内容而不关闭底层流
zipOutputStream.finish();
if (null != zipOutputStream) {
// 关闭输出流,异常直接抛出
zipOutputStream.close();
}
}
}
}
package com.sadhu.service;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @ClassName ZipService
* @Description TODO
* @Author sadhu
* @Date 2022/2/25 9:06
**/
@Service
public class ZipService {
/**
* 将文件流写入输出流
*
* @param zipOutputStream
*/
public void downLoadZipFile(ZipOutputStream zipOutputStream) {
// 将两个文件压入同一个压缩包
String filePath = "C:\\Users\\ASUS\\Desktop\\sadhu";
String fileName_1 = "test_1.txt";
String fileName_2 = "test_2.txt";
// 文件输入流
FileInputStream fileInputStream_1 = null;
FileInputStream fileInputStream_2 = null;
// 缓冲区
byte[] buf = new byte[1024];
try {
// 获取文件输入流
fileInputStream_1 = new FileInputStream(filePath + "\\" + fileName_1);
fileInputStream_2 = new FileInputStream(filePath + "\\" + fileName_2);
// 在zip包创建fileName_1的文件
zipOutputStream.putNextEntry(new ZipEntry(fileName_1));
// 从头开始,将文件输入流转为zip包输出流
int read = 0;
while ((read = fileInputStream_1.read(buf)) != -1) {
zipOutputStream.write(buf, 0, read);
}
// 关闭当前的ZIP条目并定位流以读取下一个条目
zipOutputStream.closeEntry();
// --------------------------
// 在zip包创建fileName_2的文件
zipOutputStream.putNextEntry(new ZipEntry(fileName_2));
// 从头开始,将文件输入流转为zip包输出流
read = 0;
buf = new byte[1024];
while ((read = fileInputStream_2.read(buf)) != -1) {
zipOutputStream.write(buf, 0, read);
}
// 关闭当前的ZIP条目并定位流以读取下一个条目
zipOutputStream.closeEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 在项目中,通过feign调用其他服务接口,获取到流,一样的可以将流转换成输出流,然后输出浏览器。
// 在定义feign接口的时候用 Response 对象去接返回,而对应服务返回值为void,并且将流输出给Response
Response response = "feign接口";
// 转换为输入流
InputStream inputStream = response.body().asInputStream();
// 然后再转输出流