| Upload.java |
1 /*
2 * Copyright 2005 jMethods, Inc. All rights reserved.
3 * jMethods PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4 */
5
6 package com.myjavaworld.ftp.examples;
7
8 import java.io.File;
9
10 import com.myjavaworld.ftp.ConnectionException;
11 import com.myjavaworld.ftp.FTPClient;
12 import com.myjavaworld.ftp.FTPConstants;
13 import com.myjavaworld.ftp.FTPException;
14 import com.myjavaworld.ftp.ListParser;
15 import com.myjavaworld.ftp.RemoteFile;
16
17 /**
18 * This example demonstrates how to connect to an FTP site and upload a file to
19 * the FTP server.
20 *
21 * @author Sai Pullabhotla, psai [at] jMethods [dot] com
22 * @version 1.0
23 */
24 public class Upload {
25
26 /**
27 * @param args
28 * command line arguments.
29 * @throws InstantiationException
30 * propogated
31 * @throws ClassNotFoundException
32 * propogated
33 * @throws IllegalAccessException
34 * propogated
35 * @throws FTPException
36 * propogated
37 * @throws ConnectionException
38 * propogated
39 */
40
41 public static void main(String[] args) throws InstantiationException,
42 ClassNotFoundException, IllegalAccessException, FTPException,
43 ConnectionException {
44
45 //Determine the FTPClient implementation that you would like to use.
46 //For most FTP servers, the DefaultFTPClient should work.
47 String className = "com.myjavaworld.ftp.DefaultFTPClient";
48
49 //Use reflection to find the class and obtain an instance of the class.
50 FTPClient client = (FTPClient) Class.forName(className).newInstance();
51
52 //Determine the directory list parser that we would like to use in this
53 // session. For most FTP servers, the default implementatiom should work
54 // fine.
55 ListParser parser = (ListParser) Class.forName(
56 "com.myjavaworld.ftp.DefaultListParser").newInstance();
57
58 //Set the list parser to use with the FTP client.
59 client.setListParser(parser);
60
61 //Connect to the FTP server.
62 System.out.println("Connecting...");
63
64 //Change the host name to one of your FTP servers in which you have
65 //write permissions.
66 client.connect("ftp.netscape.com");
67 System.out.println("Connected. ");
68
69 //Login to the FTP server.
70 System.out.println("Logging in...");
71 client.login("anonymous", "you@yourcompany.com");
72 System.out.println("Logged in. ");
73
74 //Upload a file
75 File source = new File("/Users/sai/temp/NSSetup.exe");
76 RemoteFile destination = parser.createRemoteFile(
77 "/pub/netscape7/english/7.1/windows/win32/NSSetup.exe", false);
78 System.out.println("Uploading: " + source);
79 client.upload(source, destination, FTPConstants.TYPE_BINARY, false, 0L);
80 System.out.println("Done. ");
81
82 //Disconnect from the FTP server.
83 System.out.println("Disconnecting...");
84 client.disconnect();
85 System.out.println("Disconnected. ");
86 }
87 }