gpt4 book ai didi

java - 如何列出来自http服务器的目录中的所有文件?

转载 作者:可可西里 更新时间:2023-11-01 16:38:26 27 4
gpt4 key购买 nike

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

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

最佳答案

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

<?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 你只需要像这样调用这个脚本:

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..
}
}

(执行异步任务:

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

)

希望对您有所帮助!

关于java - 如何列出来自http服务器的目录中的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com