Android okhttp带参数上传单张图片

private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

/**
     * 上传多张图片及参数
     * @param reqUrl URL地址
     * @param params 参数
     * @param pic_key 上传图片的关键字
     * @param paths  图片路径
     */
    public Observable sendMultipart(String reqUrl,Map params,String pic_key, List files){
        return Observable.create(new Observable.OnSubscribe(){

            @Override
            public void call(Subscriber subscriber) {
                MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
                multipartBodyBuilder.setType(MultipartBody.FORM);
                //遍历map中所有参数到builder
                if (params != null){
                    for (String key : params.keySet()) {
                        multipartBodyBuilder.addFormDataPart(key, params.get(key));
                    }
                }
                //遍历paths中所有图片绝对路径到builder,并约定key如“upload”作为后台接受多张图片的key
                if (files != null){
                    for (File file : files) {
                        multipartBodyBuilder.addFormDataPart(pic_key, file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
                    }
                }
                //构建请求体
                RequestBody requestBody = multipartBodyBuilder.build();

                Request.Builder RequestBuilder = new Request.Builder();
                RequestBuilder.url(reqUrl);// 添加URL地址
                RequestBuilder.post(requestBody);
                Request request = RequestBuilder.build();
                mOkHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        subscriber.onError(e);
                        subscriber.onCompleted();
                        call.cancel();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String str = response.body().string();
                        subscriber.onNext(str);
                        subscriber.onCompleted();
                        call.cancel();
                    }
                });
            }
        });
    }



 File file = new File(Environment.getExternalStorageDirectory()+"path");
                if (!file.exists()) {
                    Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                } else {
                    RequestBody fileBody = RequestBody.create(MediaType.parse(TYPE), file);
                    RequestBody requestBody = new MultipartBody.Builder().addFormDataPart("filename", file.getName(), fileBody).build();
                    Request requestPostFile = new Request.Builder()
                            .url("http://10.11.64.50/upload/UploadServlet")
                            .post(requestBody)
                            .build();
                    client.newCall(requestPostFile).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, final Response response) throws IOException {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    tvShow.setText(response.toString());
                                }
                            });
                        }
                    });
                }
                break;


你可能感兴趣的:(安卓类)