MultipartFile类型转换成File
方法来自于https://www.cnblogs.com/lshan/p/9557016.html
//MultipartFile类型转换成File
File file = null;
try {
String originalFilename = image_data.getOriginalFilename();
String[] filename = originalFilename.split("\\.");
file=File.createTempFile(filename[0], filename[1]);
image_data.transferTo(file);
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
//图片鉴黄
String s = checkImgUrl(file);
JSONObject jsonObject = JSONObject.parseObject(s);
String errcode = jsonObject.get("errcode").toString();
if(errcode == "" || !errcode.equals("0")){
return resultMap(Type.error, "图片不合格!", "", null, "", "501");
}
鉴黄方法
String url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" +getAccessToken();
RequestBody builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("media",file.getName(), RequestBody.create(file, MediaType.parse("*/*")))
.build();
Request request = new Request.Builder()
.url(url)
.post(builder)
.build();
try (Response response = client.newCall(request).execute()) {
return Objects.requireNonNull(response.body()).string();
} catch (Exception e) {
e.printStackTrace();
}
return "";