记一次 Spring Cloud Feign接口返回流的过程
网关项目由于过滤器的存在不能让接口直接暴露在公网,目前需要进行迂回战术,在可以公网无鉴权的项目里进行Feign调用,由此跳过鉴权,但是发现返回的是一个txt/html,之前一只返回的是数据类型,第一次使用Feign返回此类型
内部暴露
@ApiOperation("获取自助机取件码")
@GetMapping("/selfServiceMachine/getQRCode/{caseId}")
public void getQRCode(HttpServletResponse response,@PathVariable Long caseId) {
try {
SelfServiceMachine obj = selfServiceMachineService.findByCaseId(caseId);
response.setContentType("text/html");
response.getWriter().write("<html>" +
"<body>" +
"<img src='data:image/jpeg;base64," + obj.getQucikResponseCode() + "'/>" +
"</body>" +
"</html>");
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
FeignClient
/**
* @author Hope
* @date 2020/7/21 14:43
* @return Response
* @description: 获取自助机取件码
*/
@GetMapping(path = "/selfServiceMachine/getQRCode/{caseId}" ,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response getQRCode( @PathVariable(value = "caseId")Long caseId);
外部暴露
/**
* @author Hope
* @date 2020/7/21 14:43
* @return Response
* @description: 获取自助机取件码-无鉴权
*/
@GetMapping("/selfServiceMachine/getQRCode/{caseId}")
public void getQRCode(HttpServletResponse servletResponse, @PathVariable(value = "caseId") Long caseId) {
Response response = iCaseFeignClient.getQRCode(caseId);
Response.Body body = response.body();
InputStream fileInputStream = null;
OutputStream outStream;
try {
fileInputStream = body.asInputStream();
outStream = servletResponse.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(bytes)) != -1) {
outStream.write(bytes, 0, len);
}
fileInputStream.close();
outStream.close();
outStream.flush();
} catch (Exception e) {
log.error(e.getMessage());
}
}