#Java调用第三方接口获得图片输入流InputStream并返回给前端的解决方案
控制器
/**
* 获取图片验证码
*/
@PostMapping(value = {"/getImage"})
public void getVerify(HttpServletResponse response) {
AjaxResult ajaxResult = new AjaxResult();
InputStream inputStream = iApiRegisterService.getVerify();
HttpUtils.writeFile(response,inputStream);
}
util重要
/**
*
* 将输入流输出到页面
* @date: 2020年11月17日
* @param resp
* @param inputStream
*/
public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
OutputStream out = null;
try {
out = resp.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = inputStream.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
##成功解决调用第三方接口将接受到的importstream返回给前端