我正在通过 android 实现应用程序上传文件。我有一项服务:http://example.com:1001/UPLOAD/FileUpload.do .我想上传一个文件和两个类似的参数
0?event=Upload&type=:1
:0 服务器 [当前 http://example.com:1001/UPLOAD/FileUpload.do]
:1 {发票,签名}
: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>
这是我的安卓代码:
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);
但我不知道,为什么它没有上传到服务器。怎么了???
最佳答案
<form>
说event_Upload
;你只有event
.这有帮助吗?
另见 Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar
关于android - 多部分实体 POST android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19738594/