关于okhttp上传文件
老师讲的类已经被不用了,对于新的okhttp3.5.0,上传表单带图片怎么图片一直传布上去,也看了okhttp3.x的demo
public void doUpload(View view) { File file = new File(Environment.getExternalStorageDirectory(), "2.jpg"); if (!file.exists()) { L.e(file.getAbsolutePath() + "not exist!"); } RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("username", "xiaoqiang") .addFormDataPart("password", "123") .addFormDataPart("mPhoto", "2.jpg", RequestBody.create(MediaType.parse("image/jpeg"), file)) .build(); Request request = new Request.Builder() .url(BaseUrl + "uploadInfo") .post(requestBody) .build(); executeRequest(request); }
这个是我写的,可是一直报错,在服务端图片接收不到,图片是按老师写的