将图像从android上传到php服务器
问题描述:
我想从android设备上传一张照片到php网站。 为此,我使用MultipartEntity
编码。将图像从android上传到php服务器
对此I added
我的项目作为external jar file
httpmime-4.1-beta1.jar
。
我想上传到php的网站是image
存储在SDcard
。 为此我做在我的代码如下:
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" />
谢谢!
答
我建议不要使用beta
库。你应该使用apache-mime4j-0.6。您也需要httpmime-4.0.1。
这些都需要你的进口:
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
你可能在寻找这样的事情: http://stackoverflow.com/questions/5476625/android-simple-way-to -up-a-picture-to-a-image-host/5476726#5476726 – pawelzieba