MultipartEntity POST android

时间:2013-11-02 03:13:46

标签: android file-upload multipartentity

我正在通过android实现app上传文件。我有一项服务:http://example.com:1001/UPLOAD/FileUpload.do。我想上传一个文件和两个参数,如

0?event = Upload& type =:1

:0 SERVER [当前http://example.com:1001/UPLOAD/FileUpload.do]

:1 {invoice,signature}

:2表单上的文件名称:UploadedFile

当我在浏览器上复制并粘贴:http://example.com:1001/UPLOAD/FileUpload.do时,它会响应一个类似

的表单

<head></head>
<body>
    <form enctype="multipart/form-data" action="FileUpload.do" method="post">
        <input type="file" name="UploadedFile"></input>
        <input type="hidden" value="invoice" name="type"></input>
        <input type="submit" value="Upload" name="event_Upload"></input>
    </form>
</body>

这是我的代码android:

HttpClient httpClient = new DefaultHttpClient();           
HttpPost httpPost = new HttpPost("http://example.com:1001/UPLOAD/FileUpload.do");
File file = new File(pathFile);
FileBody fileBody = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("event", new StringBody("Upload"));
reqEntity.addPart("type", new StringBody("invoice"));
reqEntity.addPart("UploadedFile", fileBody);

httpPost.setEntity(reqEntity);              
httpClient.execute(httpPost);

但我不知道,为什么它不上传到服务器。怎么了???

1 个答案:

答案 0 :(得分:1)

<form>event_Upload;你刚才event。这有帮助吗?

另见Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar