Step by Step sending any file from localhost to any FTP Server

0 comments



Below are the Configuration and Setup Details for sending file from a local host machine to any FTP server.

Requirements
  • JWSDK2.0
  • JDK 1.5
  • FTP server on Remote machine
Preliminary Setup
  • Libraries
  • No external Jar files needed
One FTP server need to be installed at Remote location. Create a user and provide permission to read, write (upload).

Transferring file through FTP

This example can be used to send file from local machine to the remote machine where FTP server is running. Example uses java i/o and net package for

java.net.URL;
java.net.URLConnection;
java.io.InputStream;
java.io.OutputStream;
java.io.FileInputStream;

java.net.URLConnection is used to make URL connection with remote host according to the given user name and Password.
{
new URL("ftp://"+ user+ ":"+ password+ "@"+ host+ "/"+ targetfile+ ";type=i");
}

URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream();

FileInputStream is used to read local file and write to the remote file.

FileInputStream is= new FileInputStream(localfile);
byte[] buf= new byte[16384];
int c;
while (true)
{
c= is.read(buf);
if (c<= 0) break;
os.write(buf, 0, c);
}
os.close();
is.close();
urlc.close();

No comments:

Post a Comment

Recent Posts

Popular Posts

© 2011-2019 Web Portal Club