如何从http服务器列出目录中的所有文件? [英] How to list all files in directory from http server?

查看:1332
本文介绍了如何从http服务器列出目录中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用仅具有服务器URL的Android API从http服务器获取目录中包含的所有文件的列表。我怎样才能实现它?

I would like to get a list of all files contained in a directory from http server using Android API having only server's URL. How can i achieve it?

顺便说一句 - 如果有任何需要设置,我可以访问服务器。

By the way - I have the access to the server if there was anything needed to set up.

推荐答案

您必须编写一个PHP脚本来扫描服务器上的所有文件,例如:

You have to write a PHP-Script to scan all files on your server for example like that:

<?php
$directory = '/path/to/files';

if ( ! is_dir($directory)) {
    exit('Invalid diretory path');
}

$files = array();

foreach (scandir($directory) as $file) {
    $files[] = $file;
}

var_dump($files);  // YOU HAVE TO WRITE THE OUTPUT AS JSON OR XML
?>

使用android你必须只调用这个脚本:

With android you have to only call this script like:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

(执行asyncTask:

(To execute the asyncTask:

 new RequestTask().execute("URI to PHP Script");

希望有所帮助!

这篇关于如何从http服务器列出目录中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆