How to get info of an FTPFile - java

I am using ApacheCommons to connect to a server and get information of a specific file. I am looking for a method like FTPFile file = ftpclient.getFile(path); or something like this, so I can give it the path of the file and then be able to get info about that file. By info I mean file.getName() or file.lendth().
The only way I found is using listing methods such as listFiles(path) but it requires using for-loop and so on. But is there any better way or more straight forward?

Use the FTPClient.mlistFile:
public FTPFile mlistFile(String pathname)
Get file details using the MLST command
Of course, this will work only, if your server supports the modern MLST command.
If not, you would have to use a dedicated command for each file property. Like the SIZE for file size (not supported natively by Apache Commons) or the MDTM for file modification time (the FTPClient.getModificationTime).

Related

How to get checksum for a file on Jackrabbit server?

We have run a standard Jackrabbit WebDAV server and uploaded some files in it.
When we upload a new file with the same name we need to check if the file on the server is up to date and shouldn't be replaced. The initial idea was to use ETags but we it turned out to be too weak and is not suitable for the application.
So now the idea is to check the checksum (hash) of the incoming and existing files. As files can be pretty big and downloading everytime can be a time-consuming operation, it's better to have an option to easily obtain checksum for already uploaded files.
So my question: are there any options to get a checksum for files uploaded to the server? Maybe there are some other options or features that will help in such file handling?
There used to be Content-MD5 for that (in the HTTP spec, not Jackrabbit).
The HTTP Working Group currently works on new digest types (see https://httpwg.org/http-extensions/draft-ietf-httpbis-digest-headers.html), but this is still work-in-progress and is not implemented in Jackrabbit.
That said, getting a hash definitively could be implemented as a WebDAV property, or using a WebDAV extension report.
You can use md5 or shaxxx hashes for this purpose. I found a prebuilt MD5 Javascript library on github. I'd be shocked if there wasn't one for Java

Java - get real filename of symlink on remote HTTP Server

I am working within Java, and downloading files from a HTTP Server. Now we are working with symlinks here, so we do not need to change the http link - it is always pointing to "last-uploaded.zip" which is linked to the last uploaded zip file, as an example "package43.zip".
Do I have the chance to get the original filename within java? So the link is pointing to "last-uploaded.zip" but if it is downloaded I want to rename it to "package$version.zip".
Regards,
Marco
No. The whole symlink concept doesn't transfer over HTTP, so when you make a HTTP GET for last-uploaded.zip you don't know if it's a file, a symlink or just an endpoint that returns bytes.
The simplest solution is probably opening the zip and searching for the version number from inside there somewhere.

How do I append some text to a distant txt file via http in Java?

A text file is located at http://example.com/myText.txt with read and write permissions.
How can I append some line of text on top of it using Java. I see examples with the class OutputStreamWriter but they all use a file as an output. What about http?
(I need the code to be compatible with Java 5)
HTTP in the sense you understand it does not support any write operation of this nature.
There is no clear relationship between a URL and a file on the server; Specifically HTTP GET generally returns you a file-like resource if the URL refers to a web-like server; but the outcomes of HTTP POST are not simply an overwrite of the remote url file they are processed on a case by case basis by the other side.
If this is really what you want I strongly suggest you use FTP which is intended for this purpose.
There is no generic solution to this other than reading the content, appending the line locally, and writing it back with PUT (and that requires that the server supports the PUT method).

Delete file from a FTP server using Java

I am writing an application that involves multiple clients. One client uploads a file using FTP to a server, and then another client downloads the file and deletes it. I am using the FTP server kind of as a middleman to exchange information because I do not want the user to have to port forward. I have figured out how to upload a file, but I cannot figure out how to delete the file. The command for deleting a file using FTP is:
DELE <filename>
I have tried doing so, but with no success. Here is the code that I have tried:
public static void deleteFile(String name) throws IOException
{
URL url = new URL("ftp://a1111111:password#mywebsite.com/public_html/misc/screenshots/picture.png;type=i");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(url.openConnection().getOutputStream()));
writer.write("DELE picture.png");
writer.flush();
}
I have a feeling that the < filename > that I provided may be wrong because I did not include the directory names in the path. However, since I explicitly told the path that of the file in the URL, I am not quite sure what I am supposed to enter for the < filename >.
I have read other questions on this web site about problems very similar to this and all of the responses tell to use a library. The thing is, most of those libraries are written in pure Java so the developers of that library had to figure out a way to do what I am trying to do without a library. I want to do that as well. I do not like attaching extra files besides my own to the things that I make. So please, do not tell me to use a library - it's not what I'm looking for.
If you need any clarification, please ask!
EDIT: If I use this code to receive a response:
byte[] response = new byte[conn.getInputStream().available()];
conn.getInputStream().read(response);
System.out.println("Response: " + new String(response));
My command just gets echoed back:
Response: DELE test1.png
I think you need to do the retrieve and delete in two separate operations; some random documentation I found for FtpURLConnection says, in part:
This class Opens an FTP input (or output) stream given a URL. It works as a one shot FTP transfer :
Login
Get (or Put) the file
Disconnect
I did not see any methods in the documentation that would allow deleting a file.
You may wish to use the URL mechanism to retrieve the file, but I would drop down to using raw sockets to delete the file. Create a new connection to the FTP command port, log in, and issue the DELE command manually. If this is the only step you're taking, you might be able to get away with doing relatively poor error handling and maybe only two read() requests and simply show the output transcript to the user once you're done.
It's a bit dirty, but I completely understand not wanting to carry around a megabyte of additional source to achieve the moral equivalent of echo -e "user foo\npass password\ndele /path/to/file\nlogout" | nc ftp.remote.example.com 21.
Can you use some FTP clients to do the operation?
You can try http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html from the Apache commons-net. It is easy to use.

how to access directory from file server in java?

HI...
Currently I m working in a application in which application allows to access directory (which contains some files) from file server to Application (client).
I tried following code..
URL url=("http://192.168.5.555/file-server/user/images/");
URI uri=url.toURI();
File list[];
list= new File(uri).listFiles();
But its thrown java.lang.IllegalArgumentException Exception.
I don't know how this happen?
I simply access images directory from the given URL (file server).
Help me...
That isn't going to work. The java.io.File operates on the local disk file system only, i.e. on URI's starting with file:// only. It would otherwise indeed going to be too easy to leech files from places where you aren't allowed to do so.
Check if the server in question supports FTP, then you can just use FTPClient#listFiles() for this. If it doesn't, but it supports directory listing, then you need to parse the HTML response containing the directory listing with a HTML parser like Jsoup and then refire a new request on every found link.
If it doesn't support FTP or directory listing, then you're lost and you're probably trying to do bad things.

Categories