Source Code Cross Referenced for server2serverFTP.java in  » Net » Apache-commons-net-1.4.1 » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache commons net 1.4.1 » examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package examples;
017:
018:        import java.io.IOException;
019:        import java.io.PrintWriter;
020:        import java.net.InetAddress;
021:        import org.apache.commons.net.ProtocolCommandListener;
022:        import org.apache.commons.net.ftp.FTPClient;
023:        import org.apache.commons.net.ftp.FTPReply;
024:
025:        /***
026:         * This is an example program demonstrating how to use the FTPClient class.
027:         * This program arranges a server to server file transfer that transfers
028:         * a file from host1 to host2.  Keep in mind, this program might only work
029:         * if host2 is the same as the host you run it on (for security reasons,
030:         * some ftp servers only allow PORT commands to be issued with a host
031:         * argument equal to the client host).
032:         * <p>
033:         * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>
034:         * <p>
035:         ***/
036:        public final class server2serverFTP {
037:
038:            public static final void main(String[] args) {
039:                String server1, username1, password1, file1;
040:                String server2, username2, password2, file2;
041:                FTPClient ftp1, ftp2;
042:                ProtocolCommandListener listener;
043:
044:                if (args.length < 8) {
045:                    System.err
046:                            .println("Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>");
047:                    System.exit(1);
048:                }
049:
050:                server1 = args[0];
051:                username1 = args[1];
052:                password1 = args[2];
053:                file1 = args[3];
054:                server2 = args[4];
055:                username2 = args[5];
056:                password2 = args[6];
057:                file2 = args[7];
058:
059:                listener = new PrintCommandListener(new PrintWriter(System.out));
060:                ftp1 = new FTPClient();
061:                ftp1.addProtocolCommandListener(listener);
062:                ftp2 = new FTPClient();
063:                ftp2.addProtocolCommandListener(listener);
064:
065:                try {
066:                    int reply;
067:                    ftp1.connect(server1);
068:                    System.out.println("Connected to " + server1 + ".");
069:
070:                    reply = ftp1.getReplyCode();
071:
072:                    if (!FTPReply.isPositiveCompletion(reply)) {
073:                        ftp1.disconnect();
074:                        System.err.println("FTP server1 refused connection.");
075:                        System.exit(1);
076:                    }
077:                } catch (IOException e) {
078:                    if (ftp1.isConnected()) {
079:                        try {
080:                            ftp1.disconnect();
081:                        } catch (IOException f) {
082:                            // do nothing
083:                        }
084:                    }
085:                    System.err.println("Could not connect to server1.");
086:                    e.printStackTrace();
087:                    System.exit(1);
088:                }
089:
090:                try {
091:                    int reply;
092:                    ftp2.connect(server2);
093:                    System.out.println("Connected to " + server2 + ".");
094:
095:                    reply = ftp2.getReplyCode();
096:
097:                    if (!FTPReply.isPositiveCompletion(reply)) {
098:                        ftp2.disconnect();
099:                        System.err.println("FTP server2 refused connection.");
100:                        System.exit(1);
101:                    }
102:                } catch (IOException e) {
103:                    if (ftp2.isConnected()) {
104:                        try {
105:                            ftp2.disconnect();
106:                        } catch (IOException f) {
107:                            // do nothing
108:                        }
109:                    }
110:                    System.err.println("Could not connect to server2.");
111:                    e.printStackTrace();
112:                    System.exit(1);
113:                }
114:
115:                __main: try {
116:                    if (!ftp1.login(username1, password1)) {
117:                        System.err.println("Could not login to " + server1);
118:                        break __main;
119:                    }
120:
121:                    if (!ftp2.login(username2, password2)) {
122:                        System.err.println("Could not login to " + server2);
123:                        break __main;
124:                    }
125:
126:                    // Let's just assume success for now.
127:                    ftp2.enterRemotePassiveMode();
128:
129:                    ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2
130:                            .getPassiveHost()), ftp2.getPassivePort());
131:
132:                    // Although you would think the store command should be sent to server2
133:                    // first, in reality, ftp servers like wu-ftpd start accepting data
134:                    // connections right after entering passive mode.  Additionally, they
135:                    // don't even send the positive preliminary reply until after the
136:                    // transfer is completed (in the case of passive mode transfers).
137:                    // Therefore, calling store first would hang waiting for a preliminary
138:                    // reply.
139:                    if (ftp1.remoteRetrieve(file1)
140:                            && ftp2.remoteStoreUnique(file2)) {
141:                        //      if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
142:                        // We have to fetch the positive completion reply.
143:                        ftp1.completePendingCommand();
144:                        ftp2.completePendingCommand();
145:                    } else {
146:                        System.err
147:                                .println("Couldn't initiate transfer.  Check that filenames are valid.");
148:                        break __main;
149:                    }
150:
151:                } catch (IOException e) {
152:                    e.printStackTrace();
153:                    System.exit(1);
154:                } finally {
155:                    try {
156:                        if (ftp1.isConnected()) {
157:                            ftp1.logout();
158:                            ftp1.disconnect();
159:                        }
160:                    } catch (IOException e) {
161:                        // do nothing
162:                    }
163:
164:                    try {
165:                        if (ftp2.isConnected()) {
166:                            ftp2.logout();
167:                            ftp2.disconnect();
168:                        }
169:                    } catch (IOException e) {
170:                        // do nothing
171:                    }
172:                }
173:            }
174:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.