- 需求:因为要通过feign服务间调用显示资源服务器产生的图片资源,但是仅仅调用这个显示二维码图片的接口并不能成功调用,因此这里记录仪下我做了好久菜成功的案例
- 生产者接口
主要是以前是直接用imageIO进行输出,现在则是重新用ServletOutputStream
要加RestController
public void createCommonQRCode(HttpServletResponse response,
Long id) throws IOException {
System.out.println("id:"+id);
ServletOutputStream stream = null;
try{
SysApk sysApk = sysApkService.findById(id);
response.setContentType("image/jpeg");
stream = response.getOutputStream();
InputStream inputStream = QRCodeUtil.encode(sysApk.getApkUrl(),
"/usr/local/hc_logo.png",sysApk.getApkName(),stream,true);
try {
stream = response.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
stream.write(bytes, 0, len);
}
inputStream.close();
stream.close();
stream.flush();
} catch (Exception e) {
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(stream != null){
stream.flush();
stream.close();
}
}
}
- 消费者接口
就如网上说的,是feign.response
@FeignClient(name = "hcnet-website-1")
public interface SysQRCodeService {
@GetMapping(value = "/qrcode/createCommonQRCode",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Response createCommonQRCode( @RequestParam("response") HttpServletResponse response, @RequestParam("id") Long id );
}
- 消费者控制层接口
我暂时没找到更好的方法,只能自己在服务端在输出一边
@ApiOperation(value = "生成二维码",notes = "生成二维码")
@ApiImplicitParams({
@ApiImplicitParam(type = "query",name = "id",value = "ID",required = true)
})
@GetMapping("/qrcode/createCommonQRCode")
@CrossOrigin(origins = "*", allowCredentials = "true",allowedHeaders = "*",methods = {RequestMethod.GET, RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.OPTIONS, RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH})
@ResponseBody
public void createCommonQRCode(HttpServletResponse response1,
Long id) throws IOException {
Response response = sysQRCodeService.createCommonQRCode(response1, id);
Response.Body body = response.body();
InputStream fileInputStream = null;
OutputStream outStream;
try {
fileInputStream = body.asInputStream();
outStream = response1.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) {
}
}
- 顺带解决的问题BufferedImage转换为InputStream
BufferedImage image = QRCodeUtil.createImageAndFont(content, imgPath, text,needCompress);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageIO.write(image, FORMAT_NAME, bs);
InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
- 顺带解决的问题:Spring Boot返回二进制图片
Response response = sysQRCodeService.createCommonQRCode(response1, id);
Response.Body body = response.body();
InputStream fileInputStream = null;
OutputStream outStream;
try {
fileInputStream = body.asInputStream();
outStream = response1.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) {
}