public static void main(String[] args) throws IOException {
String s="./saas/users/123456/2022-07/7838ab88ca6544a405b26568fdd23239.png";
String substring = s.substring(6, s.length());
System.err.println(substring);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
//设置连接超时时间
.connectTimeout(100, TimeUnit.SECONDS)
//设置读取超时时间
.readTimeout(100, TimeUnit.SECONDS)
.build();
// file是要上传的文件 File()
File file2 = new File( System.getProperty("user.dir") + "/files"+"/Users/apple/Downloads" + "/人脸.jpg");
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file2);
// 不仅可以支持传文件,还可以在传文件的同时,传参数
MultipartBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "人脸.jpg", fileBody)
.build();
Request request = new Request.Builder().url("http://101.200.139.97/face/service/getId").post(requestBody).build();
Response response = okHttpClient.newCall(request).execute();
System.err.println(response.body());
if (!response.isSuccessful()) {
System.err.println("请求失败");
// 一般会在这抛个异常
}
String result = Objects.requireNonNull(response.body()).string();
System.err.println(result);
Objects.requireNonNull(response.body()).close();
}