图片上传给服务器
okhttp3提供啦MultipartBody,可以上传Multipart表单
实现上传文件以及上传参数
public static Request getFileRequest(String url,File file,Map<String, String> maps){
MultipartBody.Builder builder= new MultipartBody.Builder().setType(MultipartBody.FORM);
if(maps==null){
builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\""), RequestBody.create(MediaType.parse("image/png"),file)
).build();
}else{
for (String key : maps.keySet()) {
builder.addFormDataPart(key, maps.get(key));
}
builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\""), RequestBody.create(MediaType.parse("image/png"),file)
);
}
RequestBody body=builder.build();
return new Request.Builder().url(url).post(body).build();
}
如果是参数,则通过addFormDataPart添加 如果是文件,则通过addPart添加