【问题标题】:Upload file by HTTP POST通过 HTTP POST 上传文件
【发布时间】:2014-01-06 01:48:18
【问题描述】:

我想使用HTTP POST 将文件(特别是图像)上传到 REST 服务器。我已经导入/添加到构建路径httpmime-4.3.1.jar 和apache-mime4j-0.6.jar。我在堆栈跟踪中收到以下错误。

这有效吗? post.setHeader("enctype", "multipart/form-data");

HTTP POST 代码

public void multiPartPost() throws ClientProtocolException, IOException {
    File image = new File(EXTERNALSTORAGE + "/Pictures/sample.jpg");
    FileBody fileBody = new FileBody(image);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    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.v("multiPartPost HTTP Response", responseBody);
}

堆栈跟踪

01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to resolve static field 1755 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
01-05 17:41:25.470: D/dalvikvm(6564): VFY: replacing opcode 0x62 at 0x0000
01-05 17:41:25.470: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.create, referenced from method org.apache.http.entity.mime.content.FileBody.<init>
01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to resolve static method 6613: Lorg/apache/http/entity/ContentType;.create (Ljava/lang/String;)Lorg/apache/http/entity/ContentType;
01-05 17:41:25.470: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0000
01-05 17:41:25.470: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.create, referenced from method org.apache.http.entity.mime.content.FileBody.<init>
01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to resolve static method 6614: Lorg/apache/http/entity/ContentType;.create (Ljava/lang/String;Ljava/lang/String;)Lorg/apache/http/entity/ContentType;
01-05 17:41:25.470: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0000
01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.478: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.478: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.478: I/dalvikvm(6564): Could not find method org.apache.http.util.Args.notNull, referenced from method org.apache.http.entity.mime.content.FileBody.<init>
01-05 17:41:25.478: W/dalvikvm(6564): VFY: unable to resolve static method 6808: Lorg/apache/http/util/Args;.notNull (Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
01-05 17:41:25.478: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0005
01-05 17:41:25.478: I/dalvikvm(6564): Could not find method org.apache.http.util.Args.notNull, referenced from method org.apache.http.entity.mime.content.FileBody.writeTo
01-05 17:41:25.478: W/dalvikvm(6564): VFY: unable to resolve static method 6808: Lorg/apache/http/util/Args;.notNull (Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
01-05 17:41:25.478: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0002
01-05 17:41:25.478: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.parse, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.<init>
01-05 17:41:25.478: W/dalvikvm(6564): VFY: unable to resolve static method 6618: Lorg/apache/http/entity/ContentType;.parse (Ljava/lang/String;)Lorg/apache/http/entity/ContentType;
01-05 17:41:25.486: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0000
01-05 17:41:25.486: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.486: I/dalvikvm(6564): Could not find method org.apache.http.util.Args.notNull, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.<init>
01-05 17:41:25.486: W/dalvikvm(6564): VFY: unable to resolve static method 6808: Lorg/apache/http/util/Args;.notNull (Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
01-05 17:41:25.486: D/dalvikvm(6564): VFY: replacing opcode 0x71 at 0x0005
01-05 17:41:25.486: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.getCharset, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.getCharset
01-05 17:41:25.486: W/dalvikvm(6564): VFY: unable to resolve virtual method 6616: Lorg/apache/http/entity/ContentType;.getCharset ()Ljava/nio/charset/Charset;
01-05 17:41:25.486: D/dalvikvm(6564): VFY: replacing opcode 0x6e at 0x0002
01-05 17:41:25.486: W/dalvikvm(6564): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
01-05 17:41:25.486: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.getMimeType, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.getMediaType
01-05 17:41:25.486: W/dalvikvm(6564): VFY: unable to resolve virtual method 6617: Lorg/apache/http/entity/ContentType;.getMimeType ()Ljava/lang/String;
01-05 17:41:25.486: D/dalvikvm(6564): VFY: replacing opcode 0x6e at 0x0002
01-05 17:41:25.493: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.getMimeType, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.getMimeType
01-05 17:41:25.493: W/dalvikvm(6564): VFY: unable to resolve virtual method 6617: Lorg/apache/http/entity/ContentType;.getMimeType ()Ljava/lang/String;
01-05 17:41:25.493: D/dalvikvm(6564): VFY: replacing opcode 0x6e at 0x0002
01-05 17:41:25.493: I/dalvikvm(6564): Could not find method org.apache.http.entity.ContentType.getMimeType, referenced from method org.apache.http.entity.mime.content.AbstractContentBody.getSubType
01-05 17:41:25.493: W/dalvikvm(6564): VFY: unable to resolve virtual method 6617: Lorg/apache/http/entity/ContentType;.getMimeType ()Ljava/lang/String;
01-05 17:41:25.493: D/dalvikvm(6564): VFY: replacing opcode 0x6e at 0x0002
01-05 17:41:25.493: W/dalvikvm(6564): threadid=11: thread exiting with uncaught exception (group=0x40dc51f8)
01-05 17:41:25.501: E/AndroidRuntime(6564): FATAL EXCEPTION: AsyncTask #1
01-05 17:41:25.501: E/AndroidRuntime(6564): java.lang.RuntimeException: An error occured while executing doInBackground()
01-05 17:41:25.501: E/AndroidRuntime(6564):     at android.os.AsyncTask$3.done(AsyncTask.java:287)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:217)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.lang.Thread.run(Thread.java:856)
01-05 17:41:25.501: E/AndroidRuntime(6564): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
01-05 17:41:25.501: E/AndroidRuntime(6564):     at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at com.medusa.checkit.HTTPPostRequest.multiPartPost(HTTPPostRequest.java:79)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at com.medusa.checkit.MenuActivity$BackgroundTask.doInBackground(MenuActivity.java:79)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at com.medusa.checkit.MenuActivity$BackgroundTask.doInBackground(MenuActivity.java:1)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at android.os.AsyncTask$2.call(AsyncTask.java:273)
01-05 17:41:25.501: E/AndroidRuntime(6564):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-05 17:41:25.501: E/AndroidRuntime(6564):     ... 5 more

【问题讨论】:

    标签: java rest http-post multipartentity


    【解决方案1】:

    您似乎缺少一个依赖项。

    我的课程路径中有httpclient-4.2.5、httpcleint-cache-4.2.5、httpcore-4.2.4 和httpmime-4.2.5 以及commons-lang3-3.1 和commons-logging-1.1.2 jar。

    我也使用entity.addPart(formFieldName, new FileBody(file, "image/jpeg"));,而不是使用 header 属性。

    查看Internet media type了解更多详情

    【讨论】:

    • 这是一个不错的改变 ;)
    • 另外,我使用setHeader的原因是因为上传到REST服务器时,它不会把它放在FILES文件夹中。显然设置enctype=multipart/form-data 就是这样做的。我可以使用enctype=multipart/form-data 作为FileBody 的第二个参数吗?
    • 不知道,试试看。请注意,如果它已经在工作,那么您可能需要它。这是我在您的代码和我的代码之间可以看到的唯一(显着)差异;)
    • 刚刚测试了标题属性的方法,特别是对于REST 服务器,您需要setHeader 才能使文件最终位于正确的文件夹中。
    • 请不要编辑答案,这会破坏他在问题和更新之间不断地交流,相反,留下突出显示 API 更改的 cmets,说明发生更改的版本,因为不是每个人都会使用相同的版本...
    猜你喜欢
    • 2010-11-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2013-11-29
    相关资源
    最近更新 更多