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

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

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

我使用的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;
    }

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值