okhttp3传文件

来源:互联网 发布:霍尼矩阵控制器照片 编辑:程序博客网 时间:2024/05/16 18:00
private void sendMultipart(String s){    mOkHttpClient = new OkHttpClient();    File file = new File(s);    MediaType MEDIATYPE = MediaType.parse(judgeType(s));    MultipartBody.Builder builder = new MultipartBody.Builder()            .setType(MultipartBody.FORM)            .addFormDataPart("userId","33")            .addFormDataPart(MEDIATYPE.type(),"head.jpg",                    RequestBody.create(MEDIATYPE, file));    Request request = new Request.Builder().url(ActionBase.BASE_URL+"user/updateUserImg")            .post(builder.build())            .build();    mOkHttpClient.newCall(request).enqueue(new Callback() {        @Override        public void onFailure(Call call, IOException e) {        }        @Override        public void onResponse(Call call, Response response) throws IOException {            Log.d("wangshu", response.body().string());        }    });}private static String judgeType(String path) {    FileNameMap fileNameMap = URLConnection.getFileNameMap();    String contentTypeFor = fileNameMap.getContentTypeFor(path);    if (contentTypeFor == null) {        contentTypeFor = "application/octet-stream";    }    return contentTypeFor;}
原创粉丝点击