Android - 计算服务器目录中的文件数量
我在简单地计算服务器上目录之一中存储的文件数量时遇到了麻烦。
我是一名 Android 初学者,我知道我的代码一定犯了一个简单的错误,
File file = null;
try {
file = new File(new URI("http://myURL/directory/userImages/"));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d(TAG, "Num of Files: " + file.list().length);
我收到了 URI 异常:
Expected file scheme in URI: http://myURL/directory/userImages/
我似乎无法找出问题所在。很明显是URI的问题。
任何帮助或信息将不胜感激。提前致谢
I'm having trouble with simply counting the number of files stored on one of my directories on a server.
I'm an Android beginner and I know I must be making a simple mistake with my code
File file = null;
try {
file = new File(new URI("http://myURL/directory/userImages/"));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d(TAG, "Num of Files: " + file.list().length);
I'm getting a URI exception:
Expected file scheme in URI: http://myURL/directory/userImages/
I can't seem to find out the problem here. It's obviously a problem with the URI.
Any help or info would be greatly appreciated. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不起作用,因为您使用的
File
类的 URI 构造函数仅适用于文件 URI,如 JDK。具体来说,这意味着您的 URI 以“file://”开头,这本身意味着您只能访问本地文件(或作为系统上的本地驱动器安装的远程系统上的文件)。我不确定这段特定代码的确切上下文是什么,但我很确定您想要实现的目标将需要一些更复杂的代码。
特别是,就我现在而言,不可能通过 HTTP 获取目录。我认为您可能需要 FTP/SSH/... 访问特定系统才能解决此问题。
如果您提供更多背景信息,我(或其他人)也许能够提供更多帮助。
This won't work as the URI constructor of the
File
class that you are using only works with file URIs as documented in the JDK. Concretely this means that your URI has the start with "file://", which in itself means that you can only access local files (or files on remote systems mounted as local drives on your system).I'm not sure what the exact context is of this particular piece of code, but I'm pretty sure that what you are trying to achieve will need some more complex code.
Particularly, as far as I now, it is not possible to fetch a directory via HTTP. I think you might need FTP/SSH/... access to the particular system to solve this.
If you give some more context, I (or others) might be able to give some more help.