输出实例: 接口代码:
@RequestMapping(value = "/baseinfo")
public RestResponse infoHe(@Param("bh") String bh, @Param("fjtype") String fjtype, HttpServletResponse response) {
try {
Map<String,Object> map = houseBiz.getPictureUrl(bh,fjtype);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("ContentEncoding", "UTF-8");
headers.put("X-Gisq-Token",map.get("token").toString());
int timeOutSecend = 10000;
Map<String, String> querys = new HashMap<>();
if (!map.get("fjtype").toString().equals("{}")) {
HttpResponse PreviewFile = HttpUtil.doPost(getPicturePreviewFile, "POST", headers, querys, (Map<String, String>) map.get("fjtype"), timeOutSecend);
InputStream content = PreviewFile.getEntity().getContent();
String base = inputStream2Base64(content);
return RestResponse.ok(base);
}
} catch (Exception e) {
e.printStackTrace();
}
return RestResponse.ok("null");
}
base64工具类:
private static String inputStream2Base64(InputStream is) throws Exception {
byte[] data = null;
try {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = is.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
data = swapStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new Exception("输入流关闭异常");
}
}
}
return Base64.getEncoder().encodeToString(data);
}
另一种方式,接受图片流 调用接口直接输出图片,前端处理图片
@RequestMapping(value = "/baseinfo")
public void infoHe(@Param("bh") String bh, @Param("fjtype") String fjtype, HttpServletResponse response) {
try {
Map<String,Object> map = houseBiz.getPictureUrl(bh,fjtype);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("ContentEncoding", "UTF-8");
headers.put("X-Gisq-Token",map.get("token").toString());
int timeOutSecend = 10000;
Map<String, String> querys = new HashMap<>();
if (!map.get("fjtype").toString().equals("{}")) {
HttpResponse PreviewFile = HttpUtil.doPost(getPicturePreviewFile, "POST", headers, querys, (Map<String, String>) map.get("fjtype"), timeOutSecend);
InputStream content = PreviewFile.getEntity().getContent();
BufferedImage image = ImageIO.read(content);
if (image!= null){
ImageIO.write(image,"png",response.getOutputStream());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
|