【发布时间】:2011-10-15 18:19:30
【问题描述】:
我正在尝试将照片从 android 设备上传到 php 网站。
为此,我使用MultipartEntity 编码。
为此,我将added 作为external jar file httpmime-4.1-beta1.jar 加入我的项目。
我要上传到 php 网站的是存储在 SDcard 上的 image。
为此,我在我的代码中执行以下操作:
HttpPost httppost = new HttpPost("....the link to the webiste...");
MultipartEntity reqEntity = new MultipartEntity();
StringBody sb=new StringBody("....");
File file=new File("/sdcard/image.jpg");
FileBody bin = new FileBody(file);
reqEntity.addPart("android",bin);
reqEntity.addPart("id", sb);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String page = EntityUtils.toString(resEntity);
System.out.println("PAGE :" + page);
}
但问题在于,来自 php 服务器的响应始终是断开的链接。
接下来我想尝试的是使用
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
但不幸的是,我导入的 jar 没有 HttpMultipartMode.BROWSER_COMPATIBLE 的类。因此,如果您能指出正确的方向,我将不胜感激 - 我还应该导入什么才能使其正常工作....或者我应该如何将图像上传到服务器。
我必须说服务器是为这样上传照片而构建的:
method="post" enctype="multipart/form-data"
name="form" target="_self" id="form">
<input type="hidden" name="p" value="a" />
谢谢!
【问题讨论】:
-
你可能正在寻找这样的东西:stackoverflow.com/questions/5476625/…