我一直很头疼,如何将图像上传到服务器。这对我来说是新任务。但对我来说很困惑。我在stackoverflow上搜索并用google搜索。但是我遇到了问题。
我的目的是从sdcard上传照片并从相机拍照并上传到服务器。
在ios中,这个任务已经完成,在ios php中得到这种类型:当发布php方面简单点击此信息。
并打印像这样;
$filedata = $_FILES['photos'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
我做了什么。
// yourSelectedImage is the bitmap image.
public void SaveImage() throws Exception {
try {
String url = "http://test.com/uploadImage.php";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourSelectedImage.compress(CompressFormat.PNG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("photos", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
在Php Server中,得到这样的
$ filedata = $ _FILES ['photos'];
并在php中完成了进一步的功能。
但是我没有使用httppost发布到文件结构的多部分类型,其中文件包括图像名称,图像类型,临时名称等。
编辑:
我有改变一点代码:,但未能发布图像文件信息php服务器,用户名ok但是图像文件信息无法发布。在这种情况下,Image在服务器上成功保存,我无法获取有关php服务器的信息图像。以下是代码:
public class UploadImageHttpPost {
String testpath="/mnt/sdcard/14111.jpg";
String url = "http://test.com/uploadImage.php";
public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {
Log.w("TAG", "Start Upload" );
Log.w("TAG", "URL-> "+url );
Log.w("TAG", "Image Path-> "+imagePath );
Log.w("TAG", "User Id-> "+userId );
String responseBody;
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
FileBody encFile = new FileBody(file,"image/jpeg");
Log.w("TAG", "image file-> "+encFile );
entity.addPart("photos", encFile);
entity.addPart("UserId", new StringBody(userId));
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response from image upload->" + responseBody);
}
}
我喜欢这个:
Array
(
[UserId] => 914
)
但是下面的部分缺失意味着我没有得到,多部分帖子方法中是否有任何遗漏的代码请检查上面的java代码:
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
请你纠正我,如何发布php,上面的图片信息。
答案 0 :(得分:1)
试试这个:
File file = new File(_filePath);
MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipart.addPart("file", new FileBody(file));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse res;
URI uri = new URI(url.php);
HttpPost methodpost = new HttpPost(uri);
methodpost.addHeader("pragma","no-cache");
methodpost.setEntity(multipart);
res = httpClient.execute(methodpost);
InputStream data = res.getEntity().getContent();
当然,要完成你需要的东西。
希望这有帮助。
答案 1 :(得分:1)
使用Multipart实体上传图片
File file1 = null;
FileBody bin1 = null;
HttpPost postRequest = new HttpPost(url);
// uri for your image path
if(uri != null)
{
file1 = new File(getPath(uri));
bin1 = new FileBody(file1);
}
MultipartEntity reqEntity = new MultipartEntity();
if(bin1!=null)
reqEntity.addPart("profile_image", bin1);
post.setEntity(reqEntity);
HttpResponse httpResponse = client.execute(post);
int mResponse_code = httpResponse.getStatusLine().getStatusCode();
resEntity = httpResponse.getEntity();
response = EntityUtils.toString(resEntity);
解析你的json回应。这是非常简单的方式。