使用Google Glass和WinSCP将文件上传到服务器

时间:2014-06-09 09:35:24

标签: android sftp google-glass winscp

我是谷歌眼镜开发人员。 我想使用WinSCP将我的玻璃内存中的照片上传到php服务器。 这是当前的代码:

private void postFile(){
        try{
            // the file to be posted
            String textFile = Environment.getExternalStorageDirectory() + "/DCIM/Camera/test.txt";
            Log.v(TAG, "textFile: " + textFile);
            // the URL where the file will be posted
            String postReceiverUrl = "http://learningzone.me/build/course/en/roi.php";
            Log.v(TAG, "postURL: " + postReceiverUrl);
            // new HttpClient
            HttpClient httpClient = new DefaultHttpClient();
            // post header
            HttpPost httpPost = new HttpPost(postReceiverUrl);
            File file = new File(textFile);
            FileBody fileBody = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("file", fileBody);
            httpPost.setEntity(reqEntity);
            // execute HTTP post request
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                String responseStr = EntityUtils.toString(resEntity).trim();
                Log.v(TAG, "Response: " +  responseStr);
                // you can add an if statement here and do other actions based on the response
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这就是服务器上roi.php的脚本:

$host = "ftp.example.com";
$user = "anonymous";
$pass = "";

// You get this from the form, so you don't need to do move_uploaded_file()
$fname = "/public_html/new_file.txt";
$fcont = "content";

function ftp_writeFile($ftp, $new_file, $content, $debug=false) {
    extract((array)pathinfo($new_file));

    if (!@ftp_chdir($ftp, $dirname)) {
        return false;
    }

    $temp = tmpfile();
    fwrite($temp, $fcont);
    rewind($temp);  

    $res = @ftp_fput($ftp, $basename, $temp, FTP_BINARY);
    if ($debug) echo "a- '$new_file'".(($res)?'':" [error]")."<br/>";
    fclose($temp);

    return $res;
}

$ftp = ftp_connect($host);
if (!$ftp) echo "Could not connect to '$host'<br/>";

if ($ftp && @ftp_login($ftp, $username, $password)) {
    ftp_writeFile($ftp, $fname, $fcont, true);
} else {
    echo "Unable to login as '$username:".str_repeat('*', strlen($password))."'<br/>";
}

ftp_close($ftp);

有谁知道问题是什么? 谢谢!

1 个答案:

答案 0 :(得分:2)

您需要使用Async任务生成上传到其他线程。 Android在单线程模型上工作,并使用相同的线程来使HTTPRequest可能导致FATAL异常。创建一个异步任务并生成上传到它。

AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(<pass the required parameters here for file upload>);

private class AsyncTaskRunner extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            //Call the function to upload the file here

        }