Skip to content

Commit 5aad1de

Browse files
author
Rory Winston
committed
Revert
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/branches/NET_2_0@925541 13f79535-47bb-0310-9956-ffa450edef68
1 parent b4d9909 commit 5aad1de

File tree

1 file changed

+145
-17
lines changed

1 file changed

+145
-17
lines changed

src/main/java/examples/ftp/FTPExample.java

Lines changed: 145 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
1818
package examples.ftp;
1919

2020
import java.io.FileInputStream;
2121
import java.io.FileOutputStream;
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25-
import java.io.PrintStream;
2625
import java.io.PrintWriter;
2726

2827
import org.apache.commons.net.PrintCommandListener;
2928
import org.apache.commons.net.ftp.FTP;
3029
import org.apache.commons.net.ftp.FTPClient;
3130
import org.apache.commons.net.ftp.FTPConnectionClosedException;
32-
import org.apache.commons.net.ftp.FTPFile;
3331
import org.apache.commons.net.ftp.FTPReply;
3432

3533
/***
@@ -42,22 +40,152 @@
4240
* Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
4341
* <p>
4442
***/
45-
public final class FTPExample {
43+
public final class FTPExample
44+
{
45+
46+
public static final String USAGE =
47+
"Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
48+
"\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
49+
"\t-s store file on server (upload)\n" +
50+
"\t-b use binary transfer mode\n";
4651

47-
public static final void main(String[] args) throws IOException
52+
public static final void main(String[] args)
4853
{
49-
FTPClient client = new FTPClient();
50-
client.setControlEncoding("utf-8");
51-
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
52-
client.connect("localhost");
53-
client.login("rory", "yi6bovak");
54-
55-
PrintStream out = new PrintStream(System.out, true, "utf-8");
56-
57-
for (FTPFile file : client.listFiles())
58-
out.println(file.getName());
59-
60-
out.println("你好吗");
54+
int base = 0;
55+
boolean storeFile = false, binaryTransfer = false, error = false;
56+
String server, username, password, remote, local;
57+
FTPClient ftp;
58+
59+
for (base = 0; base < args.length; base++)
60+
{
61+
if (args[base].startsWith("-s"))
62+
storeFile = true;
63+
else if (args[base].startsWith("-b"))
64+
binaryTransfer = true;
65+
else
66+
break;
67+
}
68+
69+
if ((args.length - base) != 5)
70+
{
71+
System.err.println(USAGE);
72+
System.exit(1);
73+
}
74+
75+
server = args[base++];
76+
username = args[base++];
77+
password = args[base++];
78+
remote = args[base++];
79+
local = args[base];
80+
81+
ftp = new FTPClient();
82+
ftp.addProtocolCommandListener(new PrintCommandListener(
83+
new PrintWriter(System.out)));
84+
85+
try
86+
{
87+
int reply;
88+
ftp.connect(server);
89+
System.out.println("Connected to " + server + ".");
90+
91+
// After connection attempt, you should check the reply code to verify
92+
// success.
93+
reply = ftp.getReplyCode();
94+
95+
if (!FTPReply.isPositiveCompletion(reply))
96+
{
97+
ftp.disconnect();
98+
System.err.println("FTP server refused connection.");
99+
System.exit(1);
100+
}
101+
}
102+
catch (IOException e)
103+
{
104+
if (ftp.isConnected())
105+
{
106+
try
107+
{
108+
ftp.disconnect();
109+
}
110+
catch (IOException f)
111+
{
112+
// do nothing
113+
}
114+
}
115+
System.err.println("Could not connect to server.");
116+
e.printStackTrace();
117+
System.exit(1);
118+
}
119+
120+
__main:
121+
try
122+
{
123+
if (!ftp.login(username, password))
124+
{
125+
ftp.logout();
126+
error = true;
127+
break __main;
128+
}
129+
130+
System.out.println("Remote system is " + ftp.getSystemName());
131+
132+
if (binaryTransfer)
133+
ftp.setFileType(FTP.BINARY_FILE_TYPE);
134+
135+
// Use passive mode as default because most of us are
136+
// behind firewalls these days.
137+
ftp.enterLocalPassiveMode();
138+
139+
if (storeFile)
140+
{
141+
InputStream input;
142+
143+
input = new FileInputStream(local);
144+
145+
ftp.storeFile(remote, input);
146+
147+
input.close();
148+
}
149+
else
150+
{
151+
OutputStream output;
152+
153+
output = new FileOutputStream(local);
154+
155+
ftp.retrieveFile(remote, output);
156+
157+
output.close();
158+
}
159+
160+
ftp.logout();
161+
}
162+
catch (FTPConnectionClosedException e)
163+
{
164+
error = true;
165+
System.err.println("Server closed connection.");
166+
e.printStackTrace();
167+
}
168+
catch (IOException e)
169+
{
170+
error = true;
171+
e.printStackTrace();
172+
}
173+
finally
174+
{
175+
if (ftp.isConnected())
176+
{
177+
try
178+
{
179+
ftp.disconnect();
180+
}
181+
catch (IOException f)
182+
{
183+
// do nothing
184+
}
185+
}
186+
}
187+
188+
System.exit(error ? 1 : 0);
61189
} // end main
62190

63191
}

0 commit comments

Comments
 (0)