apache的ftpclient的使⽤
1import java.io.File;
2import java.io.FileInputStream;
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7import java.SocketException;
8import java.util.ArrayList;
9import java.util.Date;
10import java.util.List;
11import org.apachemons.ftp.FTP;
12import org.apachemons.ftp.FTPClient;
13import org.apachemons.ftp.FTPFile;
14
15public class FtpUtil {
16 private FTPClient ftpClient;
17 public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
18 public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
19
20 /**
21 * @param path 上传到ftp服务器哪个路径下
22 * @param addr 地址
23 * @param port 端⼝号
24 * @param username ⽤户名
25 * @param password 密码
26 * @return
27 * @throws Exception
28 */
29 //("", "localhost", 2121, "admin", "admin")
30 private boolean connect(String path, String addr, int port, String username, String password) throws Exception {
31 boolean result = false;
32 ftpClient = new FTPClient();
33 int reply;
34 t(addr, port);
35 ftpClient.login(username, password);
36 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
37 reply = ReplyCode();
38 if (!FTPReply.isPositiveCompletion(reply)) {
39 ftpClient.disconnect();
40 return result;
41 }
42 ftpClient.changeWorkingDirectory(path);
43 result = true;
44 return result;
45 }
46 /**
47 *@param fileType上传⽂件的类型
48 *FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
49 * @throws Exception
50 */
51 public void setFileType(int fileType) throws IOException {
52 ftpClient.setFileType(fileType);下载apache
53 }
54
55 public void closeServer() throws IOException {
56 if (ftpClient.isConnected()) {
57 ftpClient.disconnect();
58 }
59 }
60 //=======================================================================
61 //== 关于⽂件夹的操作 =====
62 // 下⾯的⽅法最好⽤相对路径为好
62 // 下⾯的⽅法最好⽤相对路径为好
63 //======================================================================= 64
65 public boolean changeDirectory(String path) throws IOException {
66 return ftpClient.changeWorkingDirectory(path);
67 }
68 public boolean createDirectory(String pathName) throws IOException {
69 return ftpClient.makeDirectory(pathName);
70 }
71 public boolean removeDirectory(String path) throws IOException {
72 veDirectory(path);
73 }
74
75 // 删除⽂件夹以及⽂件夹⾥的⽂件
76 public boolean removeDirectory(String path, boolean isAll)
77 throws IOException {
78
79 if (!isAll) {
80 return removeDirectory(path);
81 }
82
83 FTPFile[] ftpFileArr = ftpClient.listFiles(path);
84 if (ftpFileArr == null || ftpFileArr.length == 0) {
85 return removeDirectory(path);
86 }
87 //
88 for (FTPFile ftpFile : ftpFileArr) {
89 String name = Name();
90 if (ftpFile.isDirectory()) {
91 System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");
92 removeDirectory(path + "/" + name, true);
93 } else if (ftpFile.isFile()) {
94 System.out.println("* [sF]Delete file ["+path + "/" + name+"]");
95 deleteFile(path + "/" + name);
96 } else if (ftpFile.isSymbolicLink()) {
97
98 } else if (ftpFile.isUnknown()) {
99
100 }
101 }
102 veDirectory(path);
103 }
104
105 // 检查路径是否存在,存在返回true,否则false
106 public boolean existDirectory(String path) throws IOException {
107 boolean flag = false;
108 FTPFile[] ftpFileArr = ftpClient.listFiles(path);
109 for (FTPFile ftpFile : ftpFileArr) {
110 if (ftpFile.isDirectory()
111 && Name().equalsIgnoreCase(path)) {
112 flag = true;
113 break;
114 }
115 }
116 return flag;
117 }
118
119 //======================================================================= 120 //== ⽂件操作 =====
121 // 上传和下载⽂件
122 // ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better!
123 //======================================================================= 124
125 // #1. 列表和删除按操作
126 // 不包含⽂件夹
127 public List<String> getFileList(String path) throws IOException {
127 public List<String> getFileList(String path) throws IOException {
128 // listFiles return contains directory and file, it's FTPFile instance 129 // listNames() contains directory, so using following to filer directory. 130 //String[] fileNameArr = ftpClient.listNames(path);
131 FTPFile[] ftpFiles= ftpClient.listFiles(path);
132
133 List<String> retList = new ArrayList<String>();
134 if (ftpFiles == null || ftpFiles.length == 0) {
135 return retList;
136 }
137 for (FTPFile ftpFile : ftpFiles) {
138 if (ftpFile.isFile()) {
139 retList.Name());
140 }
141 }
142 return retList;
143 }
144
145 public boolean deleteFile(String pathName) throws IOException { 146 return ftpClient.deleteFile(pathName);
147 }
148
149 // #2. 上传到服务器
150 // InputStream <------> byte[] simple and See API
151
152 public boolean uploadFile(String fileName, String newName)
153 throws IOException {
154 boolean flag = false;
155 InputStream iStream = null;
156 try {
157 iStream = new FileInputStream(fileName);
158 flag = ftpClient.storeFile(newName, iStream);
159 } catch (IOException e) {
160 flag = false;
161 return flag;
162 } finally {
163 if (iStream != null) {
164 iStream.close();
165 }
166 }
167 return flag;
168 }
169
170 public boolean uploadFile(String fileName) throws IOException {
171 return uploadFile(fileName, fileName);
172 }
173
174 public boolean uploadFile(InputStream iStream, String newName) 175 throws IOException {
176 boolean flag = false;
177 try {
178 // can execute [OutputStream storeFileStream(String remote)] 179 // Above method return's value is the local file stream.
180 flag = ftpClient.storeFile(newName, iStream);
181 } catch (IOException e) {
182 flag = false;
183 return flag;
184 } finally {
185 if (iStream != null) {
186 iStream.close();
187 }
188 }
189 return flag;
190 }
191
192 // #3. 下载
192 // #3. 下载
193
194 public boolean download(String remoteFileName, String localFileName) 195 throws IOException {
196 boolean flag = false;
197 File outfile = new File(localFileName);
198 OutputStream oStream = null;
199 try {
200 oStream = new FileOutputStream(outfile);
201 flag = ieveFile(remoteFileName, oStream);
202 } catch (IOException e) {
203 flag = false;
204 return flag;
205 } finally {
206 oStream.close();
207 }
208 return flag;
209 }
210
211 public InputStream downFile(String sourceFileName) throws IOException { 212 ieveFileStream(sourceFileName);
213 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论