Java Upload Files to FTP server using URLConnection class
- Details
- Written by Nam Ha Minh
- Last Updated on 20 July 2019   |   Print Email
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:
- Java FTP file upload tutorial and example
- How to upload a directory to a FTP server
- Swing application to upload files to FTP server with progress bar
Other Java File Upload Tutorials:
- Java Servlet File Upload Example
- Spring MVC File Upload Tutorial
- Struts File Upload Example
- Java Upload files to database (Servlet + JSP + MySQL)
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP create directory example
- Java FTP example - Change working directory
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP delete file example
- Java FTP example - Search for files and directories
Comments
Not working for me.
Not working for me.
Not working for me.
Not working for me.
OBRIGADO!