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 started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



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