2

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

我的意图是从 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 服务器中,得到这样的

$filedata = $_FILES['照片'];

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

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

编辑:

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

这是代码:

公共类 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 上发布,图片的上述信息。

4

2 回答 2

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

于 2013-05-10T13:32:31.587 回答
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();

当然,完成你需要的东西。

希望这可以帮助。

于 2013-05-10T13:19:14.623 回答