Java常用FTP文件操作说明 ApacheFTPClientftp4jjftp文档格式.docx
《Java常用FTP文件操作说明 ApacheFTPClientftp4jjftp文档格式.docx》由会员分享,可在线阅读,更多相关《Java常用FTP文件操作说明 ApacheFTPClientftp4jjftp文档格式.docx(55页珍藏版)》请在冰点文库上搜索。
![Java常用FTP文件操作说明 ApacheFTPClientftp4jjftp文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-4/28/a490a369-042d-4f2f-bb15-f8a651b5d47d/a490a369-042d-4f2f-bb15-f8a651b5d47d1.gif)
16.
this.ftpClient
=
new
FTPClient();
17.
this.ftpModel
ftp;
18.
}
19.
20.
/**
21.
连接并登录FTP服务器
22.
23.
24.
boolean
ftpLogin()
25.
isLogin
false;
26.
FTPClientConfig
ftpClientConfig
FTPClientConfig(
27.
FTPClientConfig.SYST_NT);
28.
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
29.
this.ftpClient.setControlEncoding("
GBK"
);
30.
this.ftpClient.configure(ftpClientConfig);
31.
try
32.
if
(this.ftpModel.getPort()
>
0)
33.
this.ftpClient.connect(ftpModel.getUrl(),
ftpModel.getPort());
34.
}
else
35.
this.ftpClient.connect(ftpModel.getUrl());
36.
37.
FTP服务器连接回答
38.
int
reply
this.ftpClient.getReplyCode();
39.
(!
FTPReply.isPositiveCompletion(reply))
40.
this.ftpClient.disconnect();
41.
return
isLogin;
42.
43.
this.ftpClient.login(this.ftpModel.getUsername(),
this.ftpModel
44.
.getPassword());
45.
this.ftpClient.changeWorkingDirectory(this.ftpModel.getRemoteDir());
46.
this.ftpClient.setFileType(FTPClient.FILE_STRUCTURE);
47.
LogUtil.infoOutPut("
成功登陆FTP服务器:
"
+
this.ftpModel.getUrl()
端口号:
48.
this.getFtpModel().getPort()
目录:
49.
this.ftpModel.getRemoteDir());
50.
true;
51.
catch
(SocketException
e)
52.
e.printStackTrace();
53.
LogUtil.logPrint("
连接FTP服务失败!
Constants.LOG_EXCEPTION);
54.
LogUtil.logPrint(e.getMessage(),
55.
(IOException
56.
57.
登录FTP服务失败!
58.
59.
60.
System.out.println(this.ftpClient.getBufferSize());
61.
this.ftpClient.setBufferSize(1024
2);
62.
this.ftpClient.setDataTimeout(2000);
63.
64.
65.
66.
67.
退出并关闭FTP连接
68.
69.
70.
void
close()
71.
(null
!
&
this.ftpClient.isConnected())
72.
73.
reuslt
this.ftpClient.logout();
退出FTP服务器
74.
(reuslt)
75.
LogUtil.info("
退出并关闭FTP服务器的连接"
76.
77.
78.
79.
LogUtil.exception("
退出FTP服务器异常!
80.
LogUtil.exception(e.getMessage());
81.
finally
82.
83.
关闭FTP服务器的连接
84.
85.
86.
关闭FTP服务器的连接异常!
87.
88.
89.
90.
91.
92.
93.
94.
检查FTP服务器是否关闭
,如果关闭接则连接登录FTP
95.
96.
@return
97.
98.
isOpenFTPConnection()
99.
isOpen
100.
==
this.ftpClient)
101.
102.
103.
104.
没有连接
105.
106.
this.ftpLogin();
107.
108.
(Exception
109.
110.
FTP服务器连接登录异常!
111.
112.
113.
114.
isOpen;
115.
116.
117.
118.
设置传输文件的类型[文本文件或者二进制文件]
119.
120.
@param
fileType--FTPClient.BINARY_FILE_TYPE,FTPClient.ASCII_FILE_TYPE
121.
122.
setFileType(int
fileType)
123.
124.
this.ftpClient.setFileType(fileType);
125.
126.
127.
设置传输文件的类型异常!
128.
129.
130.
131.
132.
133.
下载文件
134.
135.
localFilePath
136.
本地文件名及路径
137.
remoteFileName
138.
远程文件名称
139.
140.
141.
downloadFile(String
localFilePath,
String
remoteFileName)
142.
BufferedOutputStream
outStream
null;
143.
success
144.
145.
BufferedOutputStream(new
FileOutputStream(
146.
localFilePath));
147.
this.ftpClient.retrieveFile(remoteFileName,
outStream);
148.
(FileNotFoundException
149.
150.
151.
152.
153.
(outStream
null)
154.
155.
outStream.flush();
156.
outStream.close();
157.
158.
159.
160.
161.
162.
success;
163.
164.
165.
166.
167.
168.
169.
本地文件
170.
171.
172.
173.
174.
downloadFile(File
localFile,
175.
176.
FileOutputStream
outStr
177.
178.
179.
FileOutputStream(localFile);
180.
BufferedOutputStream(outStr);
181.
182.
183.
184.
185.
186.
187.
188.
outStream)
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
outStr)
200.
201.
outStr.flush();
202.
outStr.close();
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
上传文件
215.
216.
217.
本地文件路径及名称
218.
219.
FTP
服务器文件名称
220.
221.
222.
uploadFile(String
223.
BufferedInputStream
inStream
224.
225.
226.
BufferedInputStream(new
FileInputStream(
227.
228.
this.ftpClient.storeFile(remoteFileName,
inStream);
229.
230.
231.
232.
233.
234.
(inStream
235.
236.
inStream.close();
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
uploadFile(File
255.
inStrea