# Version requirements

FizzGate integrated platform v3.1.0 or above (Installation Tutorial (opens new window))

The FizzGate integration platform has supported the forwarding of file download requests since version 1.0, and has supported downloading files in the service orchestration function since v3.1.0 to facilitate more complex interface orchestration.

# Environment preparation

Create a service to simulate the existing interface, prepare two files attendance.xlsx and image1.jpg and place them in the tmp directory of the simulated service. The code:

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");
// Here we read local files in order to simulate the stream
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");
// Here we read local files in order to simulate the stream
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();
}

}

  • Download excel file interface URL: http://127.0.0.1:8080/download/excel

  • Download image interface URL: http://127.0.0.1:8080/download/image

The configuration of downloading excel and downloading pictures for service orchestration is the same. The following uses downloading excel as an example for configuration.

# Arrange download file interface

# Add new interface

Menu location: Service Edit->Interface List, click Add

# Configuration input

In the configuration input tab, you can define the input parameters and request header information of the interface. If you do not define it, the gateway will not perform any verification on the received parameters. In this example, no parameters are required, so leave them all blank.

# Configuration steps

Double-click the request node to open the node editing page:

Click Add HTTP Service to add the file download service to the system.

Select the newly added service fizz-examples-rest-api, fill in the download file interface path /download/excel, and leave the configuration response blank. If left blank, the result returned by the interface will be retained as is.

# Configure output

Configure the response message to be returned to the front end. The results of the steps are directly quoted here.

  1. Configure the response body. To download a file, you need to use the ~tilde symbol to receive a reference to the file:

  1. Configure the response header. Generally, use the response header returned by the request node in the *star transparent transmission step.

If you need to customize the request header, if you modify the file name attendance2.xlsx, you can configure the Content-Disposition response header and the format is as follows:

attachment;filename=attendance2.xlsx

# Save and publish

After configuring the interface and clicking Save, apply for release, review, and release of the interface:

Configure routing:

Access the official interface, URL: http://[gateway IP]:8600/proxy/dl/excel