okhttp多部分图像上传文件名

时间:2014-11-12 13:20:51

标签: android http mobile okhttp

我正在尝试从Android手机上传图像到服务器。这就是我到目前为止所做的事情

  OkHttpClient client = new OkHttpClient();
            MultipartBuilder builder = new MultipartBuilder();


builder.type(MultipartBuilder.FORM).addPart(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestPackage.getJsonParam().toString()));
            for (int i = 0; i < requestPackage.getPics().size(); i++) {
                builder.addPart(RequestBody.create(MediaType.parse("image/png"/* + i*/), new File(URI.create(requestPackage.getPics().get(i)))));
            Log.i("image to upload",URI.create(requestPackage.getPics().get(i)).toString());
            }
            requestBody = builder.build();
     Request request = new Request.Builder().url(requestPackage.getUri()).post(requestBody).build();
            try {
                response = client.newCall(request).execute();
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    //            System.out.println(response.body().string());
                return response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }

如何为不同的部分添加名称。因为如果没有名称(密钥),那么服务器端的人将如何存储它?

5 个答案:

答案 0 :(得分:12)

获取OkHttp 2.1,并使用MultipartBuilder.addFormDataPart(),它将文件名作为参数。

答案 1 :(得分:5)

自从之前的答案以来,语法似乎发生了一些变化。我正在使用OkHttp 3.2.0。

public void upload(String url, File file) throws IOException {
    RequestBody formBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("file", file.getName(),
            RequestBody.create(MediaType.parse("image/png"), file))
        .addFormDataPart("other_field", "other_field_value")
        .build();
    Request request = new Request.Builder().url(url).post(formBody).build();
    Response response = this.client.newCall(request).execute();
}

答案 2 :(得分:0)

您可以在官方文档中找到所有内容:https://github.com/square/okhttp/wiki/Recipes

特别是你会对Posting a multipart request

中的作品感兴趣
RequestBody requestBody = new MultipartBuilder()
    .type(MultipartBuilder.FORM)
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"title\""),
        RequestBody.create(null, "Square Logo"))
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"image\""),
        RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
    .build();

答案 3 :(得分:0)

关于如何使用okhttp3上传文件,这是一个完整的解决方案。 首先,在点击监听器上添加一个带有按钮的文件选择器,如下所示:

用于选择文件的按钮:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_choose_file:
        showFileChooser();
        break;
    }
}

private String filePath = null;
private File sourceFile;
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

然后像这样处理onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                Uri uri = data.getData();

                File   file = new File(getCacheDir(), getFileName(uri));

                int maxBufferSize = 1 * 1024 * 1024;

                try {
                    InputStream inputStream = getContentResolver().openInputStream(uri);
                    Log.e("InputStream Size","Size " + inputStream);
                    int  bytesAvailable = inputStream.available();
                    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    final byte[] buffers = new byte[bufferSize];

                    FileOutputStream outputStream = new FileOutputStream(file);
                    int read = 0;
                    while ((read = inputStream.read(buffers)) != -1) {
                        outputStream.write(buffers, 0, read);
                    }
                    Log.e("File Size","Size " + file.length());
                    inputStream.close();
                    outputStream.close();

                    file.getPath();
                    Log.e("File Path","Path " + file.getPath());
                    file.length();
                    Log.e("File Size","Size " + file.length());

                    if(file.length() > 0){

                        sourceFile = file;
                        filePath = sourceFile.getPath();
                    }


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                }



            } else {


            }

            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


private String getMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null)
    {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}


public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

最后,处理文件上传以及其他所需的信息,例如:

  try {

       UpdateInformation("yourEmailAddress", filePath, sourceFile);

      } catch (IOException e) {
                    e.printStackTrace();
      }


private void UploadInformation(String email, final String _filePath, final File file) throws IOException {


    runOnUiThread(new Runnable() {
        @Override
        public void run() {


        //show progress bar here

        }
    });


    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();





    String mime = getMimeType(_filePath);


    RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse(mime), file))
            .addFormDataPart("email", email)
            .build();





    okhttp3.Request request = new okhttp3.Request.Builder()
            .url("yourEndPointURL")
            .post(body)
            .addHeader("authorization", "yourEndPointToken")
            .addHeader("content-type", "application/json")
            .build();



    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            call.cancel();


            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                //hide progress bar here

                }
            });

        }

        @Override
        public void onResponse(Call call, okhttp3.Response response) throws IOException {


            try {

                final String myResponse = response.body().string();


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                //hide progress bar here

                //Cont from here
                //Handle yourEndPoint Response.



                    }
                });


            } catch (Exception e) {
                e.printStackTrace();
            }


        }



    });
}

注意:请不要忘记将此权限添加到清单文件中。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 4 :(得分:-1)

您可以使用下面的多部分在单个请求中发送多个值

        HttpPost    httppost = new HttpPost(mPostURL);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("value", new StringBody("upload", Charset.forName("UTF-8")));
        File myFile = new File(mFilePath);
        FileBody fileBody = new FileBody(filePath);
        entity.addPart("file", fileBody);
        entity.addPart("filename", new StringBody("fileName", Charset.forName("UTF-8")));
        httppost.setEntity(entity);
        HttpClient httpClient = new DefaultHttpClient();