Is there any way to access http://<someserver.com>:<portnumber>/logs.log.txt using Java code in windows
I am trying to get file attributes present in a Unix server and when I type this url in my IE it displays the files in the file-folder-directory architecture.
I am planning to write a code for a tool such that I can automate the process of getting the file attributes like file modified date,size of file etc.
Are t开发者_C百科here any Methods/ways to do this? Does this code work:
File file = New File("http://<someserver.com>:<portnumber>/logs/log.txt");
Date date = file.LastModifiedDate();
System.out.println("modifed date is"+date);
If the protocol your server supports is only HTTP, I'm afraid there's no easy way to do this. You will have to:
- parse the returned HTML, probably looking for
<a href=
tags (using some html parser, but not with regex) - open those links with
new URL(url).openConnection()
, read their streams, and do the same thing recursively, until an actual file (and not directory) is found.
But this won't give you the file attributes - only the name and the file contents.
If you want to browse, you need a different protocol, like FTP or SCP.
The HTTP protocol won't help you here. HTTP does not publish any file attributes, except for the Content-length
(file size) and the Last-Modified
header value which doesn't necessarily mirror the actual file modification date. Also it might not be sent by the HTTP Server at all.
Your best bet would be using an FTP library for example the one from Apache Commons Net.
If you decide to use this library, you can use the properties of the FTPFile class, for example the file size, file date and permissions.
精彩评论