如题所述,主要想问下有没有同行的大佬帮忙看看这个问题。主要代码贴下:
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"}
问客服说估计是我的问题,但我看了半天也没看出来
文档里面的格式是这样的:
{ "media_id" : "file752398_7983424" }
media_id现在就是这样的,有啥问题?
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());