如何使用多部分上传图片到PHP服务器(How to upload image in to php s

2019-09-02 12:18发布

我一直头疼,如何上传图像中server.It是新的任务me.but大混乱了我,我搜索了计算器和Google搜索。 但我得到的问题。

我的本意是从SD卡上传照片并从相机拍摄照片并上传到服务器。

在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完成。

但我不张贴在以上类型的文件结构,其中文件包括图像名,图像类型,临时名称,以及其他人的多用httppost。

编辑:

我有变化不大位代码:,但未能发布,图像文件信息PHP服务器,用户标识确定,但图像文件的信息无法post.In这种情况下,图像保存成功在服务器上,我不能让图像的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,上述图像信息。

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

当然,要完成你所需要的。

希望这可以帮助。



Answer 2:

使用多部分实体上传图片

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



文章来源: How to upload image in to php server using Multipart