java调用图片接口服务获取图片流,转base64前端展示
java调用图片接口,获取图片流,前端展示
·
输出实例:
接口代码:
@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);
}
// BufferedImage image = ImageIO.read(content);
// if (image!= null){
// ImageIO.write(image,"png",response.getOutputStream());
// }
} catch (Exception e) {
e.printStackTrace();
}
return RestResponse.ok("null");
}
base64工具类:
/**
* 将inputstream转为Base64
*
* @param is
* @return
* @throws Exception
*/
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) {
// List<HouseInfo> list = houseBiz.listALl(houseVo);
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();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)