001.
002.
import
sun.net.*;
003.
import
sun.net.ftp.*;
004.
import
java.io.*;
005.
import
java.util.*;
006.
007.
008.
009.
010.
011.
012.
013.
014.
015.
016.
017.
018.
019.
020.
021.
022.
023.
public
class
FtpUtils {
024.
private
FtpClient ftpclient;
025.
private
String ipAddress;
026.
private
int
port;
027.
private
String username;
029.
030.
031.
032.
033.
034.
035.
036.
037.
public
FtpUtils(String ip,
int
port, String username, String password)
throws
038.
Exception {
039.
this
.ipAddress = ip;
040.
this
.port = port;
041.
this
.ftpclient =
new
FtpClient(ipAddress, port);
042.
this
.username = username;
043.
this
.password = password;
044.
}
045.
046.
047.
048.
049.
050.
051.
052.
public
FtpUtils(String ip, String username, String password)
throws
053.
Exception {
054.
this
(ip,
21
,username,password);
055.
}
056.
057.
058.
059.
060.
public
void
login()
throws
Exception {
061.
ftpclient.login(username, password);
062.
}
063.
064.
065.
066.
067.
068.
public
void
logout()
throws
Exception {
069.
070.
ftpclient.sendServer(
"QUIT\r\n"
);
071.
int
reply = ftpclient.readServerResponse();
072.
}
073.
074.
075.
076.
077.
078.
079.
080.
public
ArrayList fileNames(String fullPath)
throws
Exception {
081.
ftpclient.ascii();
082.
TelnetInputStream list = ftpclient.nameList(fullPath);
083.
byte
[] names =
new
byte
[
2048
];
084.
int
bufsize =
0
;
085.
bufsize = list.read(names,
0
, names.length);
086.
list.close();
087.
ArrayList namesList =
new
ArrayList();
088.
int
i =
0
;
089.
int
j =
0
;
090.
while
(i < bufsize ) {
091.
if
(names[i] ==
10
) {
092.
String tempName =
new
String(names, j, i - j);
093.
namesList.add(tempName);
094.
095.
j = i +
1
;
096.
}
097.
i = i +
1
;
098.
}
099.
return
namesList;
100.
}
101.
102.
103.
104.
105.
106.
107.
public
void
buildRemoteDir(String pathList)
throws
Exception {
108.
ftpclient.ascii();
109.
StringTokenizer s =
new
StringTokenizer(pathList,
"/"
);
110.
int
count = s.countTokens();
111.
String pathName =
""
;
112.
while
(s.hasMoreElements()) {
113.
pathName = pathName +
"/"
+ (String) s.nextElement();
114.
try
{
115.
ftpclient.sendServer(
"XMKD "
+ pathName +
"\r\n"
);
116.
}
catch
(Exception e) {
117.
e =
null
;
118.
}
119.
int
reply = ftpclient.readServerResponse();
120.
}
121.
ftpclient.binary();
122.
}
123.
124.
125.
126.
127.
128.
129.
130.
public
void
upFile(String local, String remote)
throws
Exception {
131.
buildRemoteDir(remote.substring(
0
, remote.lastIndexOf(
"/"
)));
132.
ftpclient.binary();
133.
TelnetOutputStream ftpOut = ftpclient.put(remote);
134.
InputStream in =
new
FileInputStream(local);
135.
byte
[] buf =
new
byte
[
204800
];
136.
int
bufsize =
0
;
137.
while
((bufsize = in.read(buf,
0
, buf.length)) != -
1
){
138.
ftpOut.write(buf,
0
, bufsize);
139.
}
140.
in.close();
141.
ftpOut.close();
142.
}
143.
public
void
buildLocalDir(String fullPath)
throws
Exception {
144.
145.
if
(fullPath.lastIndexOf(
"/"
)<=
0
)
return
;
146.
String path=fullPath.substring(
0
,fullPath.lastIndexOf(
"/"
));
147.
148.
File f=
new
File(path);
149.
if
(!f.exists()){
150.
f.mkdirs();
151.
}
152.
}
153.
public
void
downFile(String remote,String local)
throws
Exception {
154.
buildLocalDir(local);
155.
ftpclient.binary();
156.
OutputStream out=
new
FileOutputStream(
new
File(local));
157.
TelnetInputStream ftpIn = ftpclient.get(remote);
158.
byte
[] buff=
new
byte
[
204800
];
159.
int
len=
0
;
160.
while
((len=ftpIn.read(buff))!=-
1
){
161.
out.write(buff,
0
,len);
162.
}
163.
out.close();
164.
ftpIn.close();
165.
}
166.
public
static
void
main(String args[])
throws
Exception{
167.
FtpUtils upfile=
new
FtpUtils(
"192.168.187.130"
,
"root"
,
"1-1=0"
);
168.
upfile.login();
169.
List list=upfile.fileNames(
"/"
);
170.
System.out.println(list);
171.
upfile.upFile(
"FtpUtils.java"
,
"/root/xjs/test/FtpUtils.java"
);
172.
upfile.downFile(
"/root/xjs/2.txt"
,
"xjs/2.txt"
);
173.
upfile.logout();
174.
}
175.
}
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.