---
url: "https://pay.weixin.qq.com/doc/v3/partner/4012467223-java.md"
title: "图片请求接口-请求示例-java"
doc_id: 4012467223
example_lang: "java"
---

## Java


需配合微信支付工具库 WXPayUtility 使用，请参考 [Java](https://pay.weixin.qq.com/doc/v3/partner/4014985777.md)

```
package com.java.demo;

import com.java.utils.WXPayUtility; // 引用微信支付工具库，参考：https://pay.weixin.qq.com/doc/v3/partner/4014985777

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Expose;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片请求接口
 */
public class QueryImages {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "GET";
  private static String PATH = "/v3/merchant-service/images/{media_id}";

  public static void main(String[] args) {
    // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
    QueryImages client = new QueryImages(
      "19xxxxxxxx",                    // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
      "1DDE55AD98Exxxxxxxxxx",         // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
      "/path/to/apiclient_key.pem"     // 商户API证书私钥文件路径，本地文件路径
    );

    QueryImagesRequest request = new QueryImagesRequest();
    request.mediaId = "ChsyMDAwMDAwMjAyNDExMDgxNTAyMjQ4MTU1MTgYBSCIk8y5BigBMAE4AQ%3D%3D";
    try {
        Response response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
    } catch (WXPayUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public Response run(QueryImagesRequest request) {
    String uri = PATH;
    uri = uri.replace("{media_id}", WXPayUtility.urlEncode(request.mediaId));

    Request.Builder reqBuilder = new Request.Builder().url(HOST + uri);
    reqBuilder.addHeader("Accept", "*/*");

    reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null));
    reqBuilder.method(METHOD, null);
    Request httpRequest = reqBuilder.build();

    // 发送HTTP请求
    OkHttpClient client = new OkHttpClient.Builder().build();
    try {
      Response httpResponse = client.newCall(httpRequest).execute();
      if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
        return httpResponse;
      } else {
        String respBody = WXPayUtility.extractBody(httpResponse);
        httpResponse.close(); // 非200时主动关闭
        throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers());
      }
    } catch (IOException e) {
      throw new UncheckedIOException("Sending request to " + uri + " failed.", e);
    }
  }

  private final String mchid;
  private final String certificateSerialNo;
  private final PrivateKey privateKey;

  public QueryImages(String mchid, String certificateSerialNo, String privateKeyFilePath) {
    this.mchid = mchid;
    this.certificateSerialNo = certificateSerialNo;
    this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath);
  }

  public static class QueryImagesRequest {
    @SerializedName("media_id")
    @Expose(serialize = false)
    public String mediaId;
  }

}

```
