调用第三方接口返回图片给前端

Controller层

@GetMapping(value = "/query/map")
    @ApiOperation("根据经纬度查询是否存在格点")
    public void queryMap(@RequestParam("service") String service,
                         @RequestParam("version") String version,
                         @RequestParam("request") String request,
                         @RequestParam("format") String format,
                         @RequestParam("transparent") String transparent,
                         @RequestParam("layers") String layers,
                         @RequestParam("srs") String srs,
                         @RequestParam("styles") String styles,
                         @RequestParam("width") String width,
                         @RequestParam("height") String height,
                         @RequestParam("bbox") String bbox,HttpServletResponse response
                         )
            throws IOException {
        String url="SERVICE"+"="+service+"&"+
                "VERSION"+"="+version+"&"+
                "REQUEST"+"="+request+"&"+
                "FORMAT"+"="+format+"&"+
                "TRANSPARENT"+"="+transparent+"&"+
                "LAYERS"+"="+layers+"&"+
                "SRS"+"="+srs+"&"+
                "STYLES"+"="+styles+"&"+
                "WIDTH"+"="+width+"&"+
                "HEIGHT"+"="+height+"&"+
                "BBOX"+"="+bbox;

        contentService.queryMap(url,response);

controller层改用map类型接收参数

 public void queryMap(@RequestParam Map<String,String> paramsMap, HttpServletResponse response
    ) throws IOException {
        StringBuilder url = new StringBuilder();
        for (Map.Entry<String, String> m : paramsMap.entrySet()) {
            url.append(m.getKey()).append("=").append(m.getValue()).append("&");
        }
        contentService.queryMap(url.toString(),response);

    }

Service

 @Override
    public void queryMap(String url,HttpServletResponse response) throws IOException {
        System.out.println(url);

        GetUtil.sendGut("被调用方根目录?"+url,response);
    }
    

Util

public class GetUtil {
    public static void  sendGut(String url, HttpServletResponse response) throws IOException {

        InputStream is = null;
        URL urls = new URL(url);
        //打开和url之间的连接
        HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
        //请求方式
        conn.setRequestMethod("GET");

        //获取URLConnection对象对应的输出流
        conn.connect();
        //获取URLConnection对象对应的输入流
        is = conn.getInputStream();

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = is.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();
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值