Tuesday, July 26, 2011

Java Source Code to Upload a File into FTP Server

Here is the simple java code which will help uploading a file into a destination ftp server.

@Credit: Parvesh Mehla

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JOptionPane;

public class UploadIntoFTP {

      UploadIntoFTP() {

      }

      private void doUpload() {

            // This is exact location of the file in the local server/machine
            // Source Location of the file
            File fileSource = new File("C:/demo.txt");

            // this is the file name only; not the lcoation
            String fileName = fileSource.getName();

            // Username and password to enter in to the ftp server
            String userName = "username";
            String password = "password";

            // this is the ftpserver details upto the folder in which file needs to
            // be uploaded
            String ftpServer = "ftpserverlocation/folderlocation";

            System.out.println(fileName);

            // Constructing the string buffer
            StringBuffer sb = new StringBuffer("ftp://");
            // check for authentication else assume its anonymous access.

            sb.append(userName);
            sb.append(':');
            sb.append(password);
            sb.append('@');

            sb.append(ftpServer);
            sb.append('/');
            sb.append(fileName);

            /*
             * UPloading mode type ==> a=ASCII mode, i=image (binary) mode, d= file
             * directory listing
             */
            sb.append(";type=a");

            System.out.println(sb.toString());

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {

                  URL url = new URL(sb.toString());
                  System.out.println(url);
                  URLConnection urlc = url.openConnection();
                  bos = new BufferedOutputStream(urlc.getOutputStream());
                  bis = new BufferedInputStream(new FileInputStream(fileSource));

                  int i;

                  // read byte by byte until end of stream
                  while ((i = bis.read()) != -1) {
                        bos.write(i);
                  }
                  JOptionPane.showMessageDialog(null, fileName + " is uploaded",
                              "Press OK", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {

                  if (bis != null)
                        try {
                              bis.close();
                        } catch (IOException ioe) {
                              ioe.printStackTrace();
                              System.out.println("IO exception after if bis " + ioe);
                        }
                  if (bos != null)
                        try {
                              bos.close();
                        } catch (IOException ioe) {
                              ioe.printStackTrace();
                              System.out.println("IO exception after if " + ioe);
                        }
            }
      }

      public static void main(String args[]) {
            UploadIntoFTP uploadIntoftp = new UploadIntoFTP();
            uploadIntoftp.doUpload();
      }
}

No comments:

Total Pageviews