What is FTP (File ), simple java FTP file upload code

codingbeginners_ftp_fileuploading_usingJavaCode.jpg

        Today I want to share FTP (File Transfer Protocol) concept. In country, most sectors will be changed to paperless work and paperless system security is also important. File transfer protocol is extremely vital to enterprise-class processes. 
        

What is FTP

        FTP is an acronym for File Transfer Protocol. As the name suggests, FTP is used to transfer files between computers on a network. You can use FTP to exchange files between computer accounts, transfer files between an account and a desktop computer, or access online software archives. Keep in mind, however, that many FTP sites are heavily used and require several attempts before connecting.

Features and Benefits

  •  Authentication mechanism :- 
These mechanism include username and password, public key authentication, digital signature etc. 
  •  Access control :- 
Server are accessible only to those with the right authorization.
  •  Data-at-motion encryption :- 
Data-in-motion encryption is usually provided through protocols like SSL or SSH.
  •  Antivirus  :- 
FTP server integrates with several antivirus software. At the time of file upload they  automatically start the virus scanning (like as Trojans and malware).
 

Java Example

Their are few steps as follows.
 

Step 1:

 
           Create a simple Java project using any Java development tool like eclipse, net-beans etc.  

Step 2:

 
           You also needed two jar files. First one is apache-commons-net.jar and the second one is commons-net-3.3-ftp.jar

If you can also download  jar file. Just click on below link
                                                     Download, jars file

Step 3:

 
          Create one package in that package simple Java class.One is a main Java class another is FTP file upload class. Copy below Java code in your project class. 
 

FTP_MainMethod.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.codingbeginners.ftp;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FTP_MainMethod {

 public void provideFTP_ServerDetails() throws Exception {

  String sFtpServer = "x.x.x.x";
  String sftpUserName = "abc";
  String sftpPassword = "abc";
  int iPortNo = 44;
  String sLocalFilePath = "C:\Users\nilesh_vispute\Desktop\lCodingBeginners_FTP.wsdl";
  String sFileName = "nilesh.wsdl";
  String sFtpFileStoreDirectory = "/home/xxx/nil";
  FTP_UploadFile ftpUploader = new FTP_UploadFile();

  ftpUploader.connectFTP(sFtpServer, sftpUserName, sftpPassword, iPortNo, sLocalFilePath, sFileName, sFtpFileStoreDirectory);

 }
 public static void main(String[] args) throws Exception {

  FTP_MainMethod ftp_Main = new FTP_MainMethod();

  ftp_Main.provideFTP_ServerDetails();
 }

}

FTP_UploadFile.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.codingbeginners.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTP_UploadFile {

 FTPClient ftp = null;

 public void connectFTP(String sFtpServer, String sFtpUserName, String sFtpPassword, int portNo, String localFilePath, String fileName, String ftp_filePath_Replc) throws Exception {
  try {

   System.out.println("---FTPUploader---" + sFtpServer + "--" + sFtpUserName + "--" + sFtpPassword);
   ftp = new FTPClient();

   ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
   int reply;

   System.out.println("-------" + ftp.getReplyCode());

   ftp.connect(sFtpServer, portNo);
   reply = ftp.getReplyCode();
   if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    throw new Exception("Exception in connecting to FTP Server");
   }
   ftp.login(sFtpUserName, sFtpPassword);
   ftp.setFileType(FTP.BINARY_FILE_TYPE);
   ftp.enterLocalPassiveMode();

   uploadFile(localFilePath, fileName, ftp_filePath_Replc);

  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void uploadFile(String localFileFullName, String fileName, String sFtpServerDir)
 throws Exception {

  System.out.println("---uploadFile---" + localFileFullName + "--" + fileName + "--" + sFtpServerDir);
  try (InputStream input = new FileInputStream(new File(localFileFullName))) {
   this.ftp.storeFile(sFtpServerDir + fileName, input);

  } catch (FileNotFoundException fnf) {
   System.out.println("File is not found...........Remaing Call Exception flow......");

  } catch (Exception e) {
   e.printStackTrace();
  }

  final boolean result = new File(localFileFullName).delete();
  System.out.println("result: |" + result + "|");

  disconnect();

 }

 public void disconnect() {

  System.out.println(".........................FTP DICONNECT");
  if (this.ftp.isConnected()) {

   try {
    this.ftp.logout();
    this.ftp.disconnect();
   } catch (IOException f) {
    // do nothing as file is already saved to server
   }
  }
 }

}

Step 4:

 Server before uploading FTP file
 
codingbeginners.com_what_is_ftp1
 
 
See the below FTP server screen screen. local directory file upload successfully on server
 
codingbeginners.com_what_is_ftp2
 
Using the above steps you can easily upload file on FTP server.
 
Sample example file upload on FTP server are also provided. Just click on below link.


        If you like this blog, then please subscribe and suppose you have any queries or suggestion then comment below. 
 

Leave a Reply

Your email address will not be published. Required fields are marked *