In the article Upload files to a FTP server we presented how to make FTP file upload using Apache Commons Net library. In this article, we are going to introduce another way: using java.net.URLConnection class to open a FTP connection from a FTP URL which has the following syntax:

ftp://user:password@host:port/path

See the detailed description for this syntax here. For example, if you want to upload a file named Project.zip to the directory /MyProjects/archive on the host www.myserver.com using user name tom and password secret, you can write a URL as follows:

ftp://tom:secret@www.myserver.com/MyProjects/archive/Project.zip;type=i

Note that the parameter ;type=iindicates the transfer mode is binary file. We should use this parameter to ensure the file is uploaded correctly.

Following is an example program that utilizes the URLConnection class for uploading a file to a FTP server:

package net.codejava.ftp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class FtpUrlUpload {

	private static final int BUFFER_SIZE = 4096;

	public static void main(String[] args) {
		String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
		String host = "www.myserver.com";
		String user = "tom";
		String pass = "secret";
		String filePath = "E:/Work/Project.zip";
		String uploadPath = "/MyProjects/archive/Project.zip";

		ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
		System.out.println("Upload URL: " + ftpUrl);

		try {
			URL url = new URL(ftpUrl);
			URLConnection conn = url.openConnection();
			OutputStream outputStream = conn.getOutputStream();
			FileInputStream inputStream = new FileInputStream(filePath);

			byte[] buffer = new byte[BUFFER_SIZE];
			int bytesRead = -1;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, bytesRead);
			}

			inputStream.close();
			outputStream.close();

			System.out.println("File uploaded");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

The method openConnection() of the URL class returns an implementation of URLConnection class depending on the protocol used in the given URL. For a FTP URL, the implementation class would be sun.net.www.protocol.ftp.FtpURLConnection. We can check this by adding the following line:

System.out.println("Class name: " + conn.getClass().getName());

Although this method is simple to use, it is less flexibility because it hides the complexity of FTP protocol. We cannot check reply code and reply message from the server, or do an upload restart if the upload failed in the middle.

 

Related Java FTP File Upload Tutorials:

 

Other Java File Upload Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (FtpUrlUpload.java)FtpUrlUpload.java[ ]1 kB

Add comment

   


Comments 

#11Rajat Purwar2019-08-02 03:09
Not working for me.
Not working for me.
Not working for me.
Not working for me.
Not working for me.
Quote
#10Aizen2018-02-06 14:50
ftpURL not working forme please help
Quote
#9Edy Enumo2017-07-21 18:36
Thank you very much! I've been look for a code that works and it worked!!! Thank you!

OBRIGADO!
Quote
#8Snehal2017-06-21 07:57
It is not working if we have '@ ' symbol in server password. And what can be done of we don't have rights to change the password... ?
Quote
#7benchaaben2017-01-10 22:07
merciiiiiiiiiiiiiiiiiiiii , thank you a looooooooooot :)))
Quote