jax-rs restful风格接口返回图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Amelia__Liu/article/details/82658737

 接口:前端不能直接掉主框架接口 所以从我后台这里中转,也就是前端调我 我调主框架接口

有个需求就是前端那边获取头像 我这边就要处理一下

我使用的jax-rs restful风格的
controller层

  /**
     * 获取用户头像
     *
     * @param pic_name 图片名称
     * @param usage    图片类型 默认传1
     * @param type     图片大小类型 0大图,1小图
     * @return
     */
    @GET
    @Path("pictures/{pic_name}/{usage}/{type}")
    @Produces(MediaType.WILDCARD)
    public Response pictures(@PathParam("pic_name") String pic_name, @PathParam("usage") String usage, @PathParam("type") String type) {
        byte[] result = HttpUtils.getPicData(LicensePlateURLConstant.picturesURL+"/"+pic_name+"/"+usage+"/"+type);//括号里面也就是我调用主框架接口 他会给我返回一个json格式的图片 
        return Response.ok(result).header("Content-Type","image/jpeg;charset=UTF-8").build();
    }

HttpUtils

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.Header;

 /**
     * get请求返回图片字节
     *
     * @param url
     * @param
     * @return
     * @throws
     */
    public static byte[] getPicData(String url) throws Exception {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);

        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {

        }

        Header[] headers = getMethod.getResponseHeaders();
        for (Header h : headers) {

        }

        byte[] responseBody = getMethod.getResponseBody();
        return responseBody;
    }

大概就这样 这也是同事写的

猜你喜欢

转载自blog.csdn.net/Amelia__Liu/article/details/82658737