使用PHP将文件从android(java)上传到服务器

您好我试图使用 PHP文件从我的 Android应用程序上传到我的服务器.

我看过这篇文章

How to upload a file using Java HttpClient library working with PHP
http://www.veereshr.com/Java/Upload
How do I send a file in Android from a mobile device to server using http?

这是我的JAVA代码

public void upload() throws Exception {

        File file = new File("data/data/com.tigo/databases/exercise");

        Log.i("file.getName()",file.getName());

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://***.***.***.***/backDatabase.PHP");

        InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file),-1);
        reqEntity.setContentType("binary/octet-stream");    

        reqEntity.setChunked(true);
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);

        if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){
            // Successfully Uploaded
            Log.i("uploaded",response.getStatusLine().toString());
        }
        else{
            // Did not upload. Add your logic here. Maybe you want to retry.
            Log.i(" not uploaded",response.getStatusLine().toString());
        }

        httpclient.getConnectionManager().shutdown();
    }

这是我的PHP代码

<?PHP


    $uploads_dir = '/tigo/databaseBackup';

    if (is_uploaded_file($_FILES['exercise']['tmp_name']))
    {

    $info =  "File ". $_FILES['exercise']['name'] ." uploaded successfully.\n";
    $file = 'emailTest.log';
    file_put_contents($file,$info,FILE_APPEND | LOCK_EX);

    move_uploaded_file ($_FILES['exercise'] ['tmp_name'],$_FILES['exercise'] ['name']);

    } 
    else 
    {

    $info =  "Possible file upload attack: ";
    $file = 'emailTest.log';
    file_put_contents($file,FILE_APPEND | LOCK_EX);

    $info =  "filename '". $_FILES['exercise']['tmp_name'] . "'.";
    file_put_contents($file,FILE_APPEND | LOCK_EX);

    print_r($_FILES);

    }

?>

在我的logcat中,我得到HTTP / 1.1 200 OK.
当我查看服务器日志时,我收到此错误

PHP Notice:  Undefined index: exercise in /var/www/backDatabase.PHP on line 23

我也试过用:

$_FILES['userfile']['name']

代替

$_FILES['exercise']['tmp_name']

我在服务器日志中遇到了同样的错误.

我想我的问题是我无法参考我上传文件.

谢谢你的帮助.

尝试多部分实体
public void upload(String filepath) throws IOException
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
     HttpPost httppost = new HttpPost("url");
     File file = new File(filepath);
     multipartentity mpEntity = new multipartentity();
     ContentBody cbFile = new FileBody(file,"image/jpeg");
     mpEntity.addPart("userfile",cbFile); 
     httppost.setEntity(mpEntity);
     System.out.println("executing request " + httppost.getRequestLine());
     HttpResponse response = httpclient.execute(httppost);
     httpentity resEntity = response.getEntity();
             // check the response and do what is required
      }

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...