将图像上载到PHP Android服务器

将图像上载到PHP Android服务器,php,android,Php,Android,我现在无法将图像上载到我的服务器。我可以在安卓设备上拍照并获取我的位置。我有以下代码将文件上载到服务器: public Boolean postFunction(File image) { String tag = "postFunction"; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(UPLOAD_URL); try {

我现在无法将图像上载到我的服务器。我可以在安卓设备上拍照并获取我的位置。我有以下代码将文件上载到服务器:

    public Boolean postFunction(File image) {
    String tag = "postFunction";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(UPLOAD_URL);

    try {
        MultipartEntity entity = new MultipartEntity();

        entity.addPart("type", new StringBody("photo"));
        entity.addPart("data", new FileBody(image));
        httppost.setEntity(entity);
        HttpResponse response = httpclient.execute(httppost);
        Log.i(tag, "picture was uploaded " + response.toString());
        return true;

    } catch (ClientProtocolException e) {
        Log.e(tag, "Client: " + e.toString());
        return false;
    } catch (IOException e) {
        Log.e(tag, "IO: " + e.toString());
        return false;
    }
}
我将图像文件传递给这个函数,它执行上载。然而,我认为错误在于服务器端

以下是我的PHP代码:

      public function upload() {
        //$type = $this->input->post('type');
        //$data = $this->input->post('data');

        $base = $_REQUEST['data'];

        echo $base;

// base64 encoded utf-8 string

        $binary = base64_decode($base);

// binary, utf-8 bytes

        header('Content-Type: bitmap; charset=utf-8');

// print($binary);
//$theFile = base64_decode($image_data);

        $file = fopen('./test.jpg', 'wb');

        fwrite($file, $binary);

        fclose($file);

        echo '<img src=test.jpg>';
    }
公共函数上传(){
//$type=$this->input->post('type');
//$data=$this->input->post('data');
$base=$_请求['data'];
echo$base;
//base64编码utf-8字符串
$binary=base64_解码($base);
//二进制,utf-8字节
标题('Content-Type:bitmap;charset=utf-8');
//打印(二进制);
//$theFile=base64\u解码($image\u数据);
$file=fopen('./test.jpg',wb');
fwrite($file,$binary);
fclose($文件);
回声';
}

文件被触摸,但仍然保持空白。我是不是遗漏了什么?请帮忙,我试着用谷歌搜索了一下,但得到了不同的结果,对我帮助不大。

你可以查看一个非常好的教程

还可以尝试以下代码

public class TryprojectActivity extends Activity {
    InputStream is;
    int pic_count = 0;
    Bitmap bitmap=null;
    FileInputStream in1,in2,in3;
    BufferedInputStream buf;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

        try {
            in1 = new FileInputStream("/sdcard/1.jpg");
        } 
        catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            in2 = new FileInputStream("/sdcard/2.jpg");
        } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    try {
        in3 = new FileInputStream("/sdcard/3.jpg");
    } 
    catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1);
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);
    byte [] imagearray1 = bao1.toByteArray();
    String ba1=Base64.encode(imagearray1);

    Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2);
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);
    byte [] imagearray2 = bao2.toByteArray();
    String ba2=Base64.encode(imagearray2);

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3);
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream();
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3);
    byte [] imagearray3 = bao3.toByteArray();
    String ba3=Base64.encode(imagearray3);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

    nameValuePairs.add(new BasicNameValuePair("image1",ba1));
    nameValuePairs.add(new BasicNameValuePair("image2",ba2));
    nameValuePairs.add(new BasicNameValuePair("image3",ba3));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php");
        UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs);
        obj.setChunked(true);
        httppost.setEntity(obj);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        //is = entity.getContent();
        httpclient.getConnectionManager().shutdown(); 
    }
    catch(Exception e){
        //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
        //CommonFunctions.showToast(ctx, "Unable to post captured image file: " +
        //e.toString());
    }
}
公共类TryprojectActivity扩展活动{
输入流为;
int picu计数=0;
位图=空;
文件输入流in1、in2、in3;
缓冲输入流buf;
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
试一试{
in1=newfileinputstream(“/sdcard/1.jpg”);
} 
捕获(FileNotFoundException e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
试一试{
in2=新文件输入流(“/sdcard/2.jpg”);
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
} 
试一试{
in3=newfileinputstream(“/sdcard/3.jpg”);
} 
捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
} 
位图bitmapOrg1=BitmapFactory.decodeStream(in1);
ByteArrayOutputStream bao1=新建ByteArrayOutputStream();
bitmapOrg1.compress(Bitmap.CompressFormat.JPEG,90,1);
字节[]imagearray1=bao1.toByteArray();
字符串ba1=Base64.encode(imagearray1);
位图bitmapOrg2=BitmapFactory.decodeStream(in2);
ByteArrayOutputStream bao2=新建ByteArrayOutputStream();
bitmapOrg2.compress(Bitmap.CompressFormat.JPEG,90,2);
字节[]imagearray2=bao2.toByteArray();
字符串ba2=Base64.encode(imagearray2);
位图bitmapOrg3=BitmapFactory.decodeStream(in3);
ByteArrayOutputStream bao3=新的ByteArrayOutputStream();
bitmapOrg3.compress(Bitmap.CompressFormat.JPEG,90,3);
字节[]imagearray3=bao3.toByteArray();
字符串ba3=Base64.encode(imagearray3);
List nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“image1”,ba1));
添加(新的BasicNameValuePair(“image2”,ba2));
添加(新的BasicNameValuePair(“image3”,ba3));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://helpdesk.cispl.com/upload_file.php");
UrlEncodedFormEntity obj=新的UrlEncodedFormEntity(nameValuePairs);
obj.setChunked(true);
httppost.setEntity(obj);
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
//is=entity.getContent();
httpclient.getConnectionManager().shutdown();
}
捕获(例外e){
//CommonFunctions.writeLOG(ctx.getClass().toString(),e.toString());
//CommonFunctions.showToast(ctx,“无法发布捕获的图像文件:”+
//e、 toString());
}
}

如果您在基于unix的平台上运行apache/php,请确保您的apache服务器对其试图存储文件的目录具有写入权限(根据您当前的代码,php脚本所在的目录)。作为快速测试,请在脚本所在的目录中执行
chmod 0777.
,查看上载的图像是否正确显示