使用Okhttp 上传文件
public void updateHeadImg(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "http://10.168.1.242:84/api/fileUp/";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("image/png");
RequestBody fileBody = RequestBody.create(mediaType, f);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", f.getName(), fileBody)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.d(TAG, "run: "+responseData);
}catch (Exception e){e.printStackTrace();}
}
}).start();
}