Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 10:11 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Ftp client demonstration
This Java tip shows how to create a simple ftp client in Java.

Code:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.StringTokenizer; import sun.net.TelnetInputStream; import sun.net.ftp.FtpClient; public class FtpClientDemo { public static int BUFFER_SIZE = 10240; private FtpClient m_client; // set the values for your server private String host = ""; private String user = ""; private String password = ""; private String sDir = ""; private String m_sLocalFile = ""; private String m_sHostFile = ""; public FtpClientDemo() { try { System.out.println("Connecting to host " + host); m_client = new FtpClient(host); m_client.login(user, password); System.out.println("User " + user + " login OK"); System.out.println(m_client.welcomeMsg); m_client.cd(sDir); System.out.println("Directory: " + sDir); m_client.binary(); System.out.println("Success."); } catch (Exception ex) { System.out.println("Error: " + ex.toString()); } } protected void disconnect() { if (m_client != null) { try { m_client.closeServer(); } catch (IOException ex) { } m_client = null; } } public static int getFileSize(FtpClient client, String fileName) throws IOException { TelnetInputStream lst = client.list(); String str = ""; fileName = fileName.toLowerCase(); while (true) { int c = lst.read(); char ch = (char) c; if (c < 0 || ch == '\n') { str = str.toLowerCase(); if (str.indexOf(fileName) >= 0) { StringTokenizer tk = new StringTokenizer(str); int index = 0; while (tk.hasMoreTokens()) { String token = tk.nextToken(); if (index == 4) try { return Integer.parseInt(token); } catch (NumberFormatException ex) { return -1; } index++; } } str = ""; } if (c <= 0) break; str += ch; } return -1; } protected void getFile() { if (m_sLocalFile.length() == 0) { m_sLocalFile = m_sHostFile; } byte[] buffer = new byte[BUFFER_SIZE]; try { int size = getFileSize(m_client, m_sHostFile); if (size > 0) { System.out.println("File " + m_sHostFile + ": " + size + " bytes"); System.out.println(size); } else System.out.println("File " + m_sHostFile + ": size unknown"); FileOutputStream out = new FileOutputStream(m_sLocalFile); InputStream in = m_client.get(m_sHostFile); int counter = 0; while (true) { int bytes = in.read(buffer); if (bytes < 0) break; out.write(buffer, 0, bytes); counter += bytes; } out.close(); in.close(); } catch (Exception ex) { System.out.println("Error: " + ex.toString()); } } protected void putFile() { if (m_sLocalFile.length() == 0) { System.out.println("Please enter file name"); } byte[] buffer = new byte[BUFFER_SIZE]; try { File f = new File(m_sLocalFile); int size = (int) f.length(); System.out.println("File " + m_sLocalFile + ": " + size + " bytes"); System.out.println(size); FileInputStream in = new FileInputStream(m_sLocalFile); OutputStream out = m_client.put(m_sHostFile); int counter = 0; while (true) { int bytes = in.read(buffer); if (bytes < 0) break; out.write(buffer, 0, bytes); counter += bytes; System.out.println(counter); } out.close(); in.close(); } catch (Exception ex) { System.out.println("Error: " + ex.toString()); } } public static void main(String argv[]) { new FtpClientDemo(); } } ------------------------------------------------------------------------------ 17. Converting a hostname to the equivalent IP address This Java tip shows how to convert a hostname to the equivalent IP address. import java.net.InetAddress; import java.net.UnknownHostException; public class GetIP { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getByName("www.java-tips.org"); } catch (UnknownHostException e) { System.exit(2); } System.out.println(address.getHostName() + "=" + address.getHostAddress()); System.exit(0); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Demonstration of the mathematical operators Java Tip java.util 0 04-17-2008 01:02 AM
TreeSet Demonstration Java Tip java.lang 0 04-15-2008 09:34 PM
Demonstration of heaps Java Tip java.lang 0 04-14-2008 10:50 PM
Simple demonstration of HashMap Java Tip java.lang 0 04-12-2008 10:48 PM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 11:18 AM


All times are GMT +3. The time now is 07:05 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org