开发使用Feign做微服开发调用客户端时,几乎都是普通接口调用,返回一些JSON数据,
今天刚好要进行Feign客户端(服务消费者)调用服务提供者的文件下载接口,记录一下!代码如下:
服务提供者:
@PostMapping(value = "/downLoadFile")
public void downloadFile(@RequestParam String path, HttpServletResponse response) {
File file = new File(path);
FileUtil.fileDownload(response, file, false);
}
FileUtil工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
public class FileUtil {
private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
/**
* 将指定文件对象写入到特定的Servlet响应对象,进而实现下载
* 一般用于下载已经存在的文件
*
* @param response 目标Servlet响应对象
* @param file 要下载的文件对象
* @param isDeleteOriginal 是否删除服务器文件原本,true下载后将删除服务器上的文件
*/
public static void fileDownload(HttpServletResponse response, File file, Boolean isDeleteOriginal) {
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
response.setContentType("multipart.form-data")
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
int length = 0;
byte[] temp = new byte[1024 * 10];
while ((length = bufferedInputStream.read(temp)) != -1) {
bufferedOutputStream.write(temp, 0, length);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedInputStream.close();
inputStream.close();
if (isDeleteOriginal) {
file.delete();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
}
Feign客户端(服务消费者)的代码
import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "file-service")
public interface UserFeignClient extends FeignClientParent {
@GetMapping(value = "/update/downLoadFile",consumes = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
Response downloadFile(@RequestParam String path);
}
Feign客户端(服务消费者)的Controller接口方法
@GetMapping(value = "/downLoadFile")
public void downloadFile(@RequestParam String path, HttpServletResponse response) {
InputStream inputStream = null;
try {
Response serviceResponse = this.userFeignClient.downloadFile(path);
Response.Body body = serviceResponse.body();
inputStream = body.asInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
response.setContentType("multipart.form-data")
response.setHeader("Content-Disposition", serviceResponse.headers().get("Content-Disposition").toString().replace("[","").replace("]",""));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
int length = 0;
byte[] temp = new byte[1024 * 10];
while ((length = bufferedInputStream.read(temp)) != -1) {
bufferedOutputStream.write(temp, 0, length);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedInputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这一段代码是为了保持服务提供者一致的文件下载输出,其中就包括文件名!
serviceResponse.headers().get("Content-Disposition").toString().replace("[","").replace("]","")
原理:就是将服务提供者的文件下载响应的响应体(文件内容)复制到服务消费者对外的文件下载响应体中