Android 如何使用多部分将图像上传到php服务器

Android 如何使用多部分将图像上传到php服务器,android,http-post,multipart,Android,Http Post,Multipart,我一直很头疼,如何将图片上传到服务器上。这对我来说是一项新任务。但对我来说很困惑。我在stackoverflow和Google上搜索。但是我遇到了问题 我的目的是从SD卡上传照片,从相机拍照并上传到服务器。 在ios中,这项任务已经完成,在ios中php获取这类信息:发布php端时简单点击此信息 像这样印刷 $filedata = $_FILES['photos']; $f = fopen(time().".txt",'wb'); fwrite($f, print_r($_R

我一直很头疼,如何将图片上传到服务器上。这对我来说是一项新任务。但对我来说很困惑。我在stackoverflow和Google上搜索。但是我遇到了问题

我的目的是从SD卡上传照片,从相机拍照并上传到服务器。

在ios中,这项任务已经完成,在ios中php获取这类信息:发布php端时简单点击此信息

像这样印刷

$filedata = $_FILES['photos'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);

我所做的

   // 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服务器中,如下所示

$filedata=$\u文件['photos']

以及在php中完成的进一步功能

但我并没有使用httppost发布到上述文件结构类型的多部分中,其中文件包括图像名称、图像类型、临时名称和其他

编辑:

我更改了一点代码:,但未能在php服务器上发布图像文件信息,userid ok但图像文件信息无法发布。在这种情况下,图像在服务器上成功保存,我无法获取图像的php服务器上的信息

代码如下:

公共类上载ImageHttpPost{

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);

    }
}
但是下面的部分缺失意味着我没有得到,在multipart post方法中是否有任何缺失的代码请检查上面的java代码:

[photos] => Array
        (
            [name] => game.png
            [type] => image/png
            [tmp_name] => /tmp/phpiQHIXQ
            [error] => 0
            [size] => 1664972
        )
请您纠正我,如何在php上发布上述图像信息。

试试这个:

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();
当然,完成你需要的


希望这有帮助。

使用多部分实体上传图像

    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响应。这是一种非常简单的方法

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();
    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);