安卓上传的图片,PHP服务器怎么接收?

时间:2021-10-04 12:18:02
以下是安卓的代码:
private String uploadPicture( String uploadFile )
 {     
     String resultcode = "1";//0表示成功 ,1表示失败
     HttpClient httpclient = new DefaultHttpClient();   
     httpclient.getParams( ).setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1 );
     HttpPost httppost = new HttpPost( actionUrl );
     File file = new File( uploadFile );      // DEBUG     
     MultipartEntity mpEntity  = new MultipartEntity( );     
     ContentBody cbFile        = new FileBody( file, "image/jpg" );     
     ContentBody cbMessage;
  try
  {
   cbMessage = new StringBody( "TEST TSET" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     ContentBody cbAccessToken;
  try
  {
   cbAccessToken = new StringBody( "sfsdfsdf" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }   
     
     mpEntity.addPart( "source",       cbFile        );//上传文件   
     mpEntity.addPart( "message",      cbMessage     );//上传信息     
     mpEntity.addPart( "access_token", cbAccessToken );//上传信息
                 
     httppost.setEntity( mpEntity );      // DEBUG     
     HttpResponse response;
  try
  {
   response = httpclient.execute( httppost );
  }
  catch (ClientProtocolException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     HttpEntity resEntity = response.getEntity( ); // DEBUG     
     if (resEntity != null) 
     {  
      try
   {
    if (EntityUtils.toString( resEntity ) == "1")
    {
     resultcode = "0";
    }
   }
   catch (org.apache.http.ParseException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
     } // end if      
     httpclient.getConnectionManager( ).shutdown( ); 
     return resultcode;
    }

以下的方法都没收到数据,求高手指点。谢谢
        print_r($_FILES);
        print_r($_POST);
        echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
        print_r($arr);

15 个解决方案

#1


既然是上传图片的,建议使用uploadify控件,网上搜索下载下,还是不错的,肯定能解决你这个问题

#2


是客户端上传的,PHP服务器接收不了。

#3


如果
$_FILES
$_POST
file_get_contents('php://input');
这三种都接收不到的话,那重点检查你的android发送程序,建议去搜一个模拟html 的file控件的东东,然后php端用$_FILES来接收。

#4


引用 楼主 qing583538 的回复:
以下是安卓的代码:
private String uploadPicture( String uploadFile )
 {     
     String resultcode = "1";//0表示成功 ,1表示失败
     HttpClient httpclient = new DefaultHttpClient();   
     httpclient.getParams( ).setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1 );
     HttpPost httppost = new HttpPost( actionUrl );
     File file = new File( uploadFile );      // DEBUG     
     MultipartEntity mpEntity  = new MultipartEntity( );     
     ContentBody cbFile        = new FileBody( file, "image/jpg" );     
     ContentBody cbMessage;
  try
  {
   cbMessage = new StringBody( "TEST TSET" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     ContentBody cbAccessToken;
  try
  {
   cbAccessToken = new StringBody( "sfsdfsdf" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }   
     
     mpEntity.addPart( "source",       cbFile        );//上传文件   
     mpEntity.addPart( "message",      cbMessage     );//上传信息     
     mpEntity.addPart( "access_token", cbAccessToken );//上传信息
                 
     httppost.setEntity( mpEntity );      // DEBUG     
     HttpResponse response;
  try
  {
   response = httpclient.execute( httppost );
  }
  catch (ClientProtocolException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     HttpEntity resEntity = response.getEntity( ); // DEBUG     
     if (resEntity != null) 
     {  
      try
   {
    if (EntityUtils.toString( resEntity ) == "1")
    {
     resultcode = "0";
    }
   }
   catch (org.apache.http.ParseException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
     } // end if      
     httpclient.getConnectionManager( ).shutdown( ); 
     return resultcode;
    }

以下的方法都没收到数据,求高手指点。谢谢
        print_r($_FILES);
        print_r($_POST);
        echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
        print_r($arr);


引用 3 楼 ahui_lcm 的回复:
如果
$_FILES
$_POST
file_get_contents('php://input');
这三种都接收不到的话,那重点检查你的android发送程序,建议去搜一个模拟html 的file控件的东东,然后php端用$_FILES来接收。





安卓一般都是怎么上传的?因为是别人做的客户端,我提供的接口,都不好说是那边的的问题。

#5


你用
print_r($_FILES);
print_r($_POST);
echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
print_r($arr);
是不会看到什么结果的
因为你似乎并没用显示返回数据的代码,也不知道返回的数据格式是否符合要求(不合要求也可能不显示)
但你这样
file_put_contents('test.txt', print_r($_FILES, 1));
file_put_contents('test.txt', print_r($_POST, 1), FILE_APPEND);
file_put_contents('test.txt', file_get_contents('php://input'), FILE_APPEND);
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('test.txt', print_r($arr, 1), FILE_APPEND);

在 test.txt 中是一定有结果的

#6


引用 5 楼 xuzuning 的回复:
你用
print_r($_FILES);
print_r($_POST);
echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
print_r($arr);
是不会看到什么结果的
因为你似乎并没用显示返回数据的代码,也不知道返回的数据格式是否符合要求(不合要求也可能不显示)
但你这样
file_put_contents('test.txt', print_r($_FILES, 1));
file_put_contents('test.txt', print_r($_POST, 1), FILE_APPEND);
file_put_contents('test.txt', file_get_contents('php://input'), FILE_APPEND);
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('test.txt', print_r($arr, 1), FILE_APPEND);

在 test.txt 中是一定有结果的


先谢谢你的回复,我试过了,还是没有。

你有没有做过安卓上传,PHP服务器接收的功能?

有例子能提供一下吗?

#7


那 test.txt 中都有什么?

#8


http://www.baidu.com/s?wd=%E5%AE%89%E5%8D%93%E4%B8%8A%E4%BC%A0&ie=utf-8

没有看到服务端有什么不同之处,请先检查你的客户端代码
可能上传并没有执行,或上传数据为空

#9


搭个环境拦截客户端到服务端的输出传输吧。

#10


上次方式改下就OK了 改成html from 那个头

#11


该回复于2013-09-22 13:05:07被管理员删除

#12


你好,我想请问你一下,你的PHP服务器端是如何接收图片的

#13


最簡單方法,把圖片轉為二禁止,然後base64 encode,然後post到php
php 獲取post後,base64 decode 再 file_put_contents 生成文件

#14


看客户端怎么给你发的一般二进制流,或者post based64编码发送
分别file_get_contents('php://input');接收 
    普通字段接收based64($_POST['picture'])就可以了

#15


该回复于2014-07-11 14:06:50被管理员删除

#1


既然是上传图片的,建议使用uploadify控件,网上搜索下载下,还是不错的,肯定能解决你这个问题

#2


是客户端上传的,PHP服务器接收不了。

#3


如果
$_FILES
$_POST
file_get_contents('php://input');
这三种都接收不到的话,那重点检查你的android发送程序,建议去搜一个模拟html 的file控件的东东,然后php端用$_FILES来接收。

#4


引用 楼主 qing583538 的回复:
以下是安卓的代码:
private String uploadPicture( String uploadFile )
 {     
     String resultcode = "1";//0表示成功 ,1表示失败
     HttpClient httpclient = new DefaultHttpClient();   
     httpclient.getParams( ).setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1 );
     HttpPost httppost = new HttpPost( actionUrl );
     File file = new File( uploadFile );      // DEBUG     
     MultipartEntity mpEntity  = new MultipartEntity( );     
     ContentBody cbFile        = new FileBody( file, "image/jpg" );     
     ContentBody cbMessage;
  try
  {
   cbMessage = new StringBody( "TEST TSET" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     ContentBody cbAccessToken;
  try
  {
   cbAccessToken = new StringBody( "sfsdfsdf" );
  }
  catch (UnsupportedEncodingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }   
     
     mpEntity.addPart( "source",       cbFile        );//上传文件   
     mpEntity.addPart( "message",      cbMessage     );//上传信息     
     mpEntity.addPart( "access_token", cbAccessToken );//上传信息
                 
     httppost.setEntity( mpEntity );      // DEBUG     
     HttpResponse response;
  try
  {
   response = httpclient.execute( httppost );
  }
  catch (ClientProtocolException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return e.getMessage();
  }     
     HttpEntity resEntity = response.getEntity( ); // DEBUG     
     if (resEntity != null) 
     {  
      try
   {
    if (EntityUtils.toString( resEntity ) == "1")
    {
     resultcode = "0";
    }
   }
   catch (org.apache.http.ParseException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return e.getMessage();
   }
     } // end if      
     httpclient.getConnectionManager( ).shutdown( ); 
     return resultcode;
    }

以下的方法都没收到数据,求高手指点。谢谢
        print_r($_FILES);
        print_r($_POST);
        echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
        print_r($arr);


引用 3 楼 ahui_lcm 的回复:
如果
$_FILES
$_POST
file_get_contents('php://input');
这三种都接收不到的话,那重点检查你的android发送程序,建议去搜一个模拟html 的file控件的东东,然后php端用$_FILES来接收。





安卓一般都是怎么上传的?因为是别人做的客户端,我提供的接口,都不好说是那边的的问题。

#5


你用
print_r($_FILES);
print_r($_POST);
echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
print_r($arr);
是不会看到什么结果的
因为你似乎并没用显示返回数据的代码,也不知道返回的数据格式是否符合要求(不合要求也可能不显示)
但你这样
file_put_contents('test.txt', print_r($_FILES, 1));
file_put_contents('test.txt', print_r($_POST, 1), FILE_APPEND);
file_put_contents('test.txt', file_get_contents('php://input'), FILE_APPEND);
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('test.txt', print_r($arr, 1), FILE_APPEND);

在 test.txt 中是一定有结果的

#6


引用 5 楼 xuzuning 的回复:
你用
print_r($_FILES);
print_r($_POST);
echo file_get_contents('php://input');
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
print_r($arr);
是不会看到什么结果的
因为你似乎并没用显示返回数据的代码,也不知道返回的数据格式是否符合要求(不合要求也可能不显示)
但你这样
file_put_contents('test.txt', print_r($_FILES, 1));
file_put_contents('test.txt', print_r($_POST, 1), FILE_APPEND);
file_put_contents('test.txt', file_get_contents('php://input'), FILE_APPEND);
$arr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents('test.txt', print_r($arr, 1), FILE_APPEND);

在 test.txt 中是一定有结果的


先谢谢你的回复,我试过了,还是没有。

你有没有做过安卓上传,PHP服务器接收的功能?

有例子能提供一下吗?

#7


那 test.txt 中都有什么?

#8


http://www.baidu.com/s?wd=%E5%AE%89%E5%8D%93%E4%B8%8A%E4%BC%A0&ie=utf-8

没有看到服务端有什么不同之处,请先检查你的客户端代码
可能上传并没有执行,或上传数据为空

#9


搭个环境拦截客户端到服务端的输出传输吧。

#10


上次方式改下就OK了 改成html from 那个头

#11


该回复于2013-09-22 13:05:07被管理员删除

#12


你好,我想请问你一下,你的PHP服务器端是如何接收图片的

#13


最簡單方法,把圖片轉為二禁止,然後base64 encode,然後post到php
php 獲取post後,base64 decode 再 file_put_contents 生成文件

#14


看客户端怎么给你发的一般二进制流,或者post based64编码发送
分别file_get_contents('php://input');接收 
    普通字段接收based64($_POST['picture'])就可以了

#15


该回复于2014-07-11 14:06:50被管理员删除