Upload File to FTP Example
This program demonstrates uploading a file to an FTP server.
Problem Statement
Write a Java program that uploads a file to an FTP server using Apache Commons Net.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.commons.net.ftp.FTPClient; |
| 3 | public class FTPUploadExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq FTP Upload Example"); |
| 6 | FTPClient ftp=new FTPClient(); |
| 7 | try { |
| 8 | ftp.connect("ftp.eduinq.com"); |
| 9 | ftp.login("user","password"); |
| 10 | FileInputStream fis=new FileInputStream("sample.txt"); |
| 11 | boolean done=ftp.storeFile("upload/sample.txt",fis); |
| 12 | fis.close(); |
| 13 | System.out.println("Eduinq Upload Success: "+done); |
| 14 | ftp.logout(); |
| 15 | ftp.disconnect(); |
| 16 | } catch(Exception e) { |
| 17 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 18 | } |
| 19 | } |
| 20 | } |
Program Output
Eduinq FTP Upload Example Eduinq Upload Success: true
Explanation
Uses storeFile() to upload file. - Requires valid path on server. - Exception handling ensures safe operations. - In enterprise systems, FTP upload transfers reports. Conclusion: Shows uploading files to FTP server.