Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to specify absolute paths in FTP URLs?

Tags:

java

url

ftp

I'm using the Java URL and URLConnection classes to upload an file to a server using FTP. I don't need to do anything other than simply upload the file, so I'd like to avoid any external libraries and I'm wary of using the non-supported sun.net.ftp class.

Is there any way to use absolute paths in the FTP connection string? I'd like to put my files in something like "/ftptransfers/..." but the FTP path is relative to the user home directory.

Sample upload code:

URL url = new URL("ftp://username:password@host/file.txt");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
OutputStream out = uc.getOutputStream() ;
out.write("THIS DATA WILL BE WRITTEN TO FILE".getBytes());
out.close();
like image 331
Brian Avatar asked Jul 21 '09 22:07

Brian


People also ask

How do I specify an FTP path?

Open a Windows explorer window (Windows key + E) and type the FTP address (ftp://domainname.com) in the file path at the top and hit Enter. Enter the username and password into the prompt window. You can save the password and the login settings to expedite future logins.

How do you specify an absolute path?

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.

How do you specify relative paths?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

What is FTP site path?

FTP locations are referenced internally using a URL-style path format, with the ftp:// prefix. The full format of this path is: ftp://<user>:<password>@<host>:<port>//<folder>


1 Answers

I did actually find out there is a semi-standard way to do it that worked for me.

Short answer: replace the leading slash with "%2F"

Long answer: per the "A FTP URL Format" document:

For example, the URL "ftp://[email protected]/%2Fetc/motd" is interpreted by FTP-ing to "host.dom", logging in as "myname" (prompting for a password if it is asked for), and then executing "CWD /etc" and then "RETR motd".

This has a different meaning from "ftp://[email protected]/etc/motd" which would "CWD etc" and then "RETR motd"; the initial "CWD" might be executed relative to the default directory for "myname".

On the other hand, "ftp://[email protected]//etc/motd", would "CWD " with a null argument, then "CWD etc", and then "RETR motd".

like image 190
Brian Avatar answered Oct 07 '22 00:10

Brian