Load multi-page image with filename

I am trying to upload an image to a server from an android phone. this is what i have done so far

  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();
            }

      

how can i add names to different parts. Because if there is no name (key) to them, then what about the server side guy?

+4


source to share


5 answers


Get OkHttp 2.1 and use MultipartBuilder.addFormDataPart()

that takes a filename as a parameter.



+12


source


The syntax seems to have changed slightly since the previous answers. I am using 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();
}

      

+5


source


You can find everything in the official doc: https://github.com/square/okhttp/wiki/Recipes

You will be especially interested in the following snippet from 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();

      

0


source


Here is a complete solution on how to upload a file using okhttp3. First, add a file picker with a click listener to your code like this:

File selection button:

@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();
    }
}

      

Then handle the onActivityResult like this:

@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;
}

      

Finally, handle the file upload along with other necessary information, for example:

  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();
            }


        }



    });
}

      

Note. Don't forget to add this permission to your manifest file.

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

      

0


source


You can use multipart like below to send multiple values ​​in one request

        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();

      

-1


source







All Articles