# 版本要求
FizzGate集成平台v3.1.0或以上版本 (安装教程 (opens new window))
FizzGate集成平台从1.0开始已支持文件下载请求的转发,从v3.1.0开始在服务编排功能对下载文件进行了支持,以便进行更复杂的接口编排。
# 环境准备
创建一个服务来模拟已有的接口,准备两个文件attendance.xlsx和image1.jpg放在模拟服务的tmp目录下,代码:
FileDownloadController.java:
package we.controller;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
public class FileDownloadController {
@GetMapping(value = "/download/excel")
public Mono<Void> downloadExcel(ServerWebExchange exchange, ServerHttpResponse response) {
File file = new File("tmp/attendance.xlsx");
// 这里为了模拟流 而去读取本地文件
FileInputStream in = null;
try {
in = new FileInputStream(file);
Flux<DataBuffer> dataBufferFlux = DataBufferUtils.readByteChannel(in::getChannel,
new DefaultDataBufferFactory(), 4096);
ZeroCopyHttpOutputMessage zeroCopyHttpOutputMessage = (ZeroCopyHttpOutputMessage) response;
HttpHeaders headers = zeroCopyHttpOutputMessage.getHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=attendance.xlsx");
MediaType application = new MediaType("application", "vnd.ms-excel", Charset.forName("UTF-8"));
headers.setContentType(application);
return zeroCopyHttpOutputMessage.writeWith(dataBufferFlux);
} catch (Exception e) {
e.printStackTrace();
}
return Mono.empty();
}
@GetMapping(value = "/download/image")
public Mono<Void> downloadImage(ServerWebExchange exchange, ServerHttpResponse response) {
File file = new File("tmp/image1.jpg");
// 这里为了模拟流 而去读取本地文件
FileInputStream in = null;
try {
in = new FileInputStream(file);
Flux<DataBuffer> dataBufferFlux = DataBufferUtils.readByteChannel(in::getChannel,
new DefaultDataBufferFactory(), 4096);
ZeroCopyHttpOutputMessage zeroCopyHttpOutputMessage = (ZeroCopyHttpOutputMessage) response;
HttpHeaders headers = zeroCopyHttpOutputMessage.getHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=image1.jpg");
MediaType application = new MediaType("application", "octet-stream", Charset.forName("UTF-8"));
headers.setContentType(application);
return zeroCopyHttpOutputMessage.writeWith(dataBufferFlux);
} catch (Exception e) {
e.printStackTrace();
}
return Mono.empty();
}
}
下载excel文件接口URL: http://127.0.0.1:8080/download/excel
下载图片接口URL: http://127.0.0.1:8080/download/image
服务编排下载excel和下载图片的配置是一样的,下面以下载excel为例子进行配置。
# 编排下载文件接口
# 新增接口
菜单位置:服务编辑->接口列表,点击新增
# 配置输入
在配置输入tab可以定义接口的入参和请求头等信息,如果不定义网关不会对接收到的参数做任何校验。本例子不用入参,全留空。
# 配置步骤
双击请求节点打开节点编辑页面:
点击新增HTTP服务,把下载文件服务的服务添加到系统。
选择刚添加的服务fizz-examples-rest-api,填写下载文件接口路径/download/excel,配置响应留空,留空会原样保留接口返回的结果。
# 配置输出
配置要返回给前端的响应报文,这里直接引用步骤的结果
1、配置响应体,下载文件需要使用~波浪符号接收文件的引用:
2、配置响应头,一般情况使用*星号透传步骤里请求节点返回的响应头即可。
如需自定义请求头,如修改文件名为attendance2.xlsx,可配置Content-Disposition响应头且格式如下:
attachment;filename=attendance2.xlsx
# 保存发布
配置完接口点击保存后, 申请发布、审核、发布接口:
配置路由:
访问正式接口,URL: http://[网关IP]:8600/proxy/dl/excel