/**
* 上传图片
*/
public static void setPicture(){
RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpeg"),new File("图片手机地址"));
MultipartBody body=new MultipartBody.Builder()
.setType(MultipartBody.FORM)//上传图片格式一般都是这个格式MediaType.parse("multipart/form-data")
.addFormDataPart("name","nate")//除了图片在上传一些东西
.addFormDataPart("filename","grile.jpg",requestBody).build();//图片服务器定义名字,
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder().url("").post(body).build();
try {
Response response=okHttpClient.newCall(request).execute();
if (response.isSuccessful()){
System.out.println(response.body().toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
原文链接:http://www.apkbus.com/blog-340477-76714.html
打开App,阅读手记