使用multipart / form-data android上传文件时出错

时间:2016-09-21 09:39:50

标签: java android file-upload

您正在尝试使用multipart / form-data上传文件。

Process: zupportdesk.desk.zupport.chatsystem, PID: 31766
java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
at zupportdesk.desk.zupport.chatsystem.Chatting.multiPartPost(Chatting.java:247)
at zupportdesk.desk.zupport.chatsystem.Chatting.onActivityResult(Chatting.java:223)
at android.app.Activity.dispatchActivityResult(Activity.java:5493)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3522)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3569)
at android.app.ActivityThread.access$1300(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

方法

/************************************ Image Selecting ******************************************/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1)
            if (resultCode == Chatting.RESULT_OK) {
                Uri selectedImage = data.getData();

                String filePath = getPath(selectedImage);
                Log.d("chatting_imagePath2", filePath);
                String file_extn = filePath.substring(filePath.lastIndexOf(".") + 1);

                if (file_extn.equals("img") || file_extn.equals("jpg") || file_extn.equals("jpeg") || file_extn.equals("gif") || file_extn.equals("png")) {
                    //FINE

                    try {
                        multiPartPost(filePath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    //NOT IN REQUIRED FORMAT
                }
            }
    }

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String imagePath = cursor.getString(column_index);
        Log.d("chatting_imagePath", imagePath);
        return cursor.getString(column_index);
    }


    public void multiPartPost(String filePath) throws ClientProtocolException, IOException {
        File image = new File(filePath);
        FileBody fileBody = new FileBody(image);

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https:// MYURL /api/chat/UploadFileByOperator");
        post.setHeader("enctype", "multipart/form-data");

        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("sampleImage", fileBody);
        post.setEntity(multipartEntity.build());

        HttpResponse response = client.execute(post);
        String responseBody = EntityUtils.toString(response.getEntity());
        Log.d("Post_image", responseBody);
    }

    /**********************************************************************************************/

摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "zupportdesk.desk.zupport.chatsystem"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }




    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude '...'
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile files('libs/gson-2.2.2.jar')
    compile files('libs/signalr-client-sdk-android.jar')
    compile files('libs/signalr-client-sdk.jar')
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:cardview-v7:24.2.0'
    compile 'br.com.liveo:navigationdrawer-material:2.5.1'
    compile 'org.java-websocket:Java-WebSocket:1.3.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile files('libs/httpmime-4.3.1.jar')
}

2 个答案:

答案 0 :(得分:1)

几天前我也遇到了同样的问题。

使用okHttp客户端解决。

你可以试试okHttp客户端..

Uploading a large file in multipart using OkHttp

答案 1 :(得分:0)

尝试在您的游戏中添加此行。

compile 'org.apache.httpcomponents:httpmime:4.3.5'