在微信公众号里面如何显示淘宝链接的图片
1:业务需求,公司想做淘宝联盟挣点钱,但是由于我们平台是微信公众号的,这样淘宝的图片链接直接使用是不行的,当时也没什么好的办法,由于公司用户量也不大,所以就在后台把图片处理成流的形式,在前端显示,好了直接贴代码
Controller
/** * 淘宝推荐商品 * @return * @throws ApiException */ @RequestMapping("/getAnswerMaterial") public ResponseModel getAnswerMaterial(Material material,HttpServletRequest request , HttpServletResponse response) throws ApiException{ String scheme = request.getScheme()+"://"+ request.getServerName(); System.out.println(scheme); try { List<AnswerMaterial> answerMaterialList = campusCircleTaoBaoApi.getAnswerMaterialList(material); for (AnswerMaterial answerMaterial : answerMaterialList) { /* answerMaterial .setPict_url("http://localhost:8080/taoBaoImage/getMainPicture?pictUrl=" + answerMaterial.getPict_url());*/ answerMaterial .setPict_url(scheme+"/taoBaoImage/getMainPicture?pictUrl=" + answerMaterial.getPict_url()); } return new ResponseModel(answerMaterialList); } catch (BizException e) { LOGGER.error("getAnswerMaterial error"); return new ResponseModel(e.getErrcode(), e.getErrm(), null); } }
Pict_url 是图片的链接 ,现在处理把以前的淘宝图片链接变成一个请求接口 以流的形式
@RequestMapping("/getMainPicture") public void getImage(String pictUrl, HttpServletRequest request, HttpServletResponse response) { try { String imageUrl ="https:"+pictUrl; URL url = new URL(imageUrl); BufferedImage image = ImageIO.read(url); response.setContentType("image/jpg"); ServletOutputStream os = response.getOutputStream(); ImageIO.write(image, "jpg", os); os.flush(); os.close(); } catch (Exception e) { LOGGER.error("getMainPicture error"); e.printStackTrace(); } }