收藏
回答

微信支付消费者投诉2.0图片上传接口返回结果是.png结尾

如题所述,主要想问下有没有同行的大佬帮忙看看这个问题。主要代码贴下:

public JsonResult upload(MultipartFile file) {
    String fileName = file.getOriginalFilename();
    String[] arr = fileName.split("\\.");
    log.info(fileName);
    log.info(arr[0]);
    String path = "tmp/";
    String uuid = UUID.randomUUID().toString() + "." + arr[1];
    File dest = new File(new File(path).getAbsolutePath()+ "/" + uuid);
    if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
    }
    InputStream is = null;
    FileSystemResource resource = new FileSystemResource(dest);
    // 创建一个临时文件
    File tempFile;
    try {
        file.transferTo(dest); // 保存文件
        is = new FileInputStream(dest);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    //文件sha256值
    String fileSha256;
    try {
        fileSha256 = DigestUtils.sha256Hex(is);

        log.info(fileSha256);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        is.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    fileName = uuid;
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("filename", fileName);
    jsonObject.put("sha256", fileSha256);
    String body = jsonObject.toString();
    log.info(body);
    String method = "POST";
    String postUrl = complaintDomain + "/v3/merchant-service/images/upload";
    HttpUrl httpurl = HttpUrl.parse(postUrl);
    String token = null;
    try {
        token = getToken(method, httpurl, body);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (SignatureException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    String authStr = schema + " " + token;
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", authStr);
    log.info("Authorization: {}", authStr);
    headers.set("Accept", "application/json");
    headers.set("Content-Type", "multipart/form-data");
    MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
    params.add("file", resource);
    params.add("meta", jsonObject);
    HttpEntity<MultiValueMap<String, Object>> strEntity = new HttpEntity<>(params, headers);
    JSONObject result = JSONObject.parseObject(restTemplate.postForObject(postUrl, strEntity, String.class));
    log.info("result: {}", result);
    Map<String, String> map = new HashMap<>();
    map.put("mediaId", result.getString("media_id"));
    return new JsonResult<>(map);
}

返回结果如下:

{"media_id":"91FE32BF33A3EC2F2CEED2BAF330C96F.png"}

问客服说估计是我的问题,但我看了半天也没看出来

回答关注问题邀请回答
收藏

2 个回答

  • 葛成
    葛成
    2023-10-26

    文档里面的格式是这样的:

    {
      "media_id" : "file752398_7983424"
    }
    
    2023-10-26
    有用
    回复
  • Memory
    Memory
    2023-10-26

    media_id现在就是这样的,有啥问题?

    2023-10-26
    有用
    回复 7
    • 葛成
      葛成
      2023-10-26
      下面的图片请求接口就有问题了,拼接URL的时候直接400了
      2023-10-26
      回复
    • 葛成
      葛成
      2023-10-26
      然后这个图片请求接口怎么使用啊,照着文档整不出来,这是我的代码:
      String method = "GET";
              String getUrl = complaintDomain + "/v3/merchant-service/images/" + mediaId;
              log.info(getUrl);
              HttpUrl httpurl = HttpUrl.parse(getUrl);
              String token = null;
              String body = "";
              try {
                  token = getToken(method, httpurl, body);
                  log.info(token);
              } catch (UnsupportedEncodingException e) {
                  throw new RuntimeException(e);
              } catch (NoSuchAlgorithmException e) {
                  throw new RuntimeException(e);
              } catch (SignatureException e) {
                  throw new RuntimeException(e);
              } catch (InvalidKeyException e) {
                  throw new RuntimeException(e);
              }
              String authStr = schema + " " + token;
              HttpHeaders headers = new HttpHeaders();
              headers.set("Authorization", authStr);
              headers.set("Accept", "application/json");
              HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(headers);
              ResponseEntity<String> res = restTemplate.exchange(getUrl, HttpMethod.GET, formEntity, String.class);
              log.info("result: {}", res);
              JSONObject result = JSONObject.parseObject(res.getBody());
      2023-10-26
      回复
    • Memory
      Memory
      2023-10-26回复葛成
      你用这个ID直接调获取??????
      2023-10-26
      回复
    • 葛成
      葛成
      2023-10-26回复Memory
      我看文档好像是这样的吧,应该怎么获取啊
      2023-10-26
      回复
    • 葛成
      葛成
      2023-10-26
      2023-10-26
      回复
    查看更多(2)
登录 后发表内容