簡體   English   中英

使用multipart / form-data android上傳文件時出錯

[英]Error while uploading file using multipart/form-data android

您好,我嘗試使用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')
}

幾天前我也遇到了同樣的問題。

通過使用okHttp Client解決。

您可以嘗試okHttp Client。

使用OkHttp分段上傳大文件

嘗試將這行添加到gradle中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM