package com.loan.utl;
import okhttp3.*;
import org.json.JSONObject;
import sun.java2d.pipe.SpanIterator;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class oktest {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
File file = new File("C:\\Users\\h\\Desktop\\nan.jpg");
File file1 = new File("C:\\Users\\h\\Desktop\\nan.jpg");
//这里是两个图片文件
RequestBody requestBody2 = RequestBody.create(MediaType.parse("application/octet-stream"), file1);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
//我觉得 第一个参数就相当于map的key 第二个参数就是文件的名字 第三个参数就是一个file
.addFormDataPart("secondImage","11", requestBody)
.addFormDataPart("firstImage","22", requestBody2)
.build();
Request request = new Request.Builder()
.url("这里是需要传输的url")
.post(multipartBody)
.addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
//这个是我访问网站的密钥 可以不写
.addHeader("X-ADVAI-KEY", "b3f4fc21c1aef35c")
.addHeader("Content-Type", "multipart/form-data")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "fdf1e235-c2ee-4e71-9c98-12b8e6827c10")
.build();
Response response ;
try {
response = client.newCall(request).execute();
//这个是hppt返回的值
String s1 = response.body().string();
System.out.println(s1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里需要导入两个包,我最开始导入了一个包所以一直在报错
java.lang.noclassdeffounderror okio/options
这个是maven导入的依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.13.0</version>
</dependency>