最近在项目中接触到ftp4j,自我充电了一下,发现实现起来还是比较简单的,在这里记录下使用方法。
首先需要下载资源,从官方下载即可,这个文件就包含jar包跟源码以及API。给出下载地址:
http://www.sauronsoftware.it/projects/ftp4j/download.php?PHPSESSID=1gn32r2t3ho11qv9vtnhgmdoe2
然后将jar放在项目的lib文件夹里(没有就新建),然后在项目的build Path中添加这个jar库,再把源码复制到项目中。这样准备工作就做完了。下面就用这个来实现一个文件的上传下载。
首先要介绍的是 FTPClient 这个类,通过 new FTPClient()可以获得一个实例。通过这个类登陆ftp,进行文件传输等各种工作。下面介绍一些方法:
connect(String,Int); 创建一个ftp链接,第一个参数是IP,第二个参数是端口号。
setPassive(boolean);设置连接方式是被动还是主动,true代表被动,false是主动。
login(String,String);登录ftp,第一个参数是用户名,第二个是密码。
currentDirectory();获取当前ftp目录路径。
createDirectory(String);创建一个目录,参数是文件夹名称。
changeDirectory(String);改变当前ftp工作路径,参数是目标路径。
ListName();获取当前ftp路径内的所有文件名称,返回的是 String[] 类型
upload(File);上传一个本地文件,参数是本地文件实例。
upload(File,Long);上传一个本地文件,可以断点续传。第一个参数是本地文件,第二个参数是已经传到ftp上的文件大小。
download(String,File);下载文件。第一个参数是ftp文件名称,第二个参数是下载后保存在本地的文件实例。
download(String,File,Long);下载文件,可以断定续传。前两个参数同上,第三个是已经下载保存到本地的文件大小。
以上几个方法就可以实现基本的ftp操作了。下面贴上Demo代码:
1 package org.tomato.test;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6
7 import it.sauronsoftware.ftp4j.FTPAbortedException;
8 import it.sauronsoftware.ftp4j.FTPClient;
9 import it.sauronsoftware.ftp4j.FTPDataTransferException;
10 import it.sauronsoftware.ftp4j.FTPException;
11 import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
12
13 public class Main {
14
15 public static final String HOST = "192.168.1.100";
16 public static final int POST = 21;
17 public static final String NAME = "test";
18 public static final String PWD = "test";
19
20 private FTPClient client = null;
21
22 public static void main(String[] args) {
23 Main ma = new Main();
24 ma.createConnection(); //创建链接
25 // ma.createFile("tests"); //创建文件夹
26 ma.upLoadFile("D:\\test.zip", "/"); //上传文件
27 for (String a : ma.getDirList()) {
28 //下载当前目录下所有文件
29 System.out.println(a);
30 File f=new File(a);
31 if(!f.isFile()){
32 continue;
33 }
34 System.out.println("--"+ma.downFile(a, new File("D:\\"+f.getName())));
35 }
36
37 ma.closeConnection(ma.client);
38 }
39
40 /**
41 * 创建链接
42 */
43 private void createConnection() {
44 System.out.println("createConnection--");
45 client = new FTPClient();
46 try {
47 client.setPassive(true);
48 client.connect(HOST, POST);
49 client.login(NAME, PWD);
50 } catch (Exception e) {
51
52 if (client != null) {
53 closeConnection(client);
54 }
55 e.printStackTrace();
56 }
57
58 }
59
60 /**
61 * 关闭连接
62 * @param client
63 * @return
64 */
65 private boolean closeConnection(FTPClient client) {
66 System.out.println("closeConnection--");
67 if (client == null) {
68 return true;
69 }
70 if (client.isConnected()) {
71 try {
72 client.disconnect(true);
73 return true;
74 } catch (Exception e) {
75 try {
76 client.disconnect(false);
77 return true;
78 } catch (Exception ex) {
79 ex.printStackTrace();
80 return false;
81 }
82 }
83
84 }
85 return true;
86 }
87
88 /**
89 * 创建目录
90 * @param name
91 */
92 private void createFile(String name) {
93 System.out.println("createFile--name=" + name);
94 try {
95 if (client == null) {
96 return;
97 }
98 String currentpath = client.currentDirectory();
99 System.out.println("currentpath=" + currentpath);
100
101
102 client.createDirectory(name);
103 } catch (IllegalStateException e) {
104 // TODO Auto-generated catch block
105 e.printStackTrace();
106 } catch (IOException e) {
107 // TODO Auto-generated catch block
108 e.printStackTrace();
109 } catch (FTPIllegalReplyException e) {
110 // TODO Auto-generated catch block
111 e.printStackTrace();
112 } catch (FTPException e) {
113 // TODO Auto-generated catch block
114 e.printStackTrace();
115 }
116 }
117
118 /**
119 * 上传文件
120 * @param localpath 本地地址
121 * @param fpath 服务器地址
122 * @return
123 */
124 private boolean upLoadFile(String localpath, String fpath) {
125 System.out.println("upLoadFile--localpath=" + localpath + "--fpath="
126 + fpath);
127 if (client == null) {
128 return false;
129 }
130 try {
131 if (!client.currentDirectory().equals(fpath)) {
132 client.changeDirectory(fpath);
133 }
134 File file = new File(localpath);
135 System.out.println("up start");
136 client.upload(file);
137 System.out.println("up end");
138 } catch (Exception e) {
139 e.printStackTrace();
140 return false;
141 }
142 return true;
143 }
144
145 /**
146 * 下载文件
147 * @param fpath 服务器文件地址
148 * @param localfile 本地文件
149 * @return
150 */
151 private boolean downFile(String fpath, File localfile) {
152 System.out.println("downFile--fpath="+fpath);
153 try {
154 if (localfile.exists() && localfile.isFile()) {
155 client.download(fpath, localfile, localfile.length(), null);
156 } else {
157 client.download(fpath, localfile);
158 }
159 return true;
160 } catch (Exception e) {
161 e.printStackTrace();
162 return false;
163 }
164
165 }
166
167 /**
168 * 获取当前路径下的文件列表
169 * @return
170 */
171 private String[] getDirList() {
172 System.out.println("getDirList--");
173 if (client == null) {
174 return null;
175 }
176 try {
177 String[] list = client.listNames();
178 return list;
179 } catch (Exception e) {
180 e.printStackTrace();
181 return null;
182 }
183
184 }
185
186 }
187