[Java] 使用 Apache的 Commons-net库 实现FTP操作
因为最近工作中需要用到FTP操作,而手上又没有现成的FTP代码。就去网上找了一下,发现大家都使用Apache的 Commons-net库中的FTPClient。
但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。
1 package com.best.oasis.util.helper;2
3 /**
4 * 封装了一些FTP操作
5 * Created by bl05973 on 2016/3/11.
6 */
7
8 import java.io.BufferedReader;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.apache.commons.net.ftp.FTP;
19 import org.apache.commons.net.ftp.FTPClient;
20 import org.apache.commons.net.ftp.FTPCmd;
21 import org.apache.commons.net.ftp.FTPFile;
22 import org.apache.commons.net.ftp.FTPReply;
23 import org.apache.log4j.Logger;
24
25 public class FTPUtil {
26 private static Logger logger = Logger.getLogger(FTPUtil.class);
27
28 private static FTPClient getConnection() {
29 FTPClient client = new FTPClient();
30 client.setControlEncoding("UTF-8");
31 client.setDataTimeout(30000);
32 client.setDefaultTimeout(30000);
33 return client;
34 }
35
36 public static FTPClient getConnection(String host) throws IOException {
37 FTPClient client = getConnection();
38 client.connect(host);
39 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
40 throw new IOException("connect error");
41 }
42 return client;
43 }
44 public static FTPClient getConnection(String host, int port) throws IOException {
45 FTPClient client = getConnection();
46 client.connect(host, port);
47 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
48 throw new IOException("connect error");
49 }
50 return client;
51 }
52
53 public static FTPClient getConnection(String host, String username, String password) throws
54 IOException {
55 FTPClient client= getConnection(host);
56 if (StringUtil.isNotBlank(username)) {
57 client.login(username, password);
58 }
59 //if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
60 // throw new IOException("login error");
61 //}
62 return client;
63 }
64
65 public static FTPClient getConnection(String host, int port, String username, String password)
66 throws IOException {
67 FTPClient client = getConnection(host, port);
68 if (StringUtil.isNotBlank(username)) {
69 client.login(username, password);
70 }
71 //if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
72 // throw new IOException("login error");
73 //}
74 return client;
75 }
76
77 /**
78 * 移动文件(若目标文件存在则不移动,并返回false)
79 */
80 public static boolean moveFile(String curFileName, String targetFileName, FTPClient client)
81 throws IOException {
82 int reply;
83 reply = client.sendCommand(FTPCmd.RNFR, curFileName);
84 if (FTPReply.isNegativePermanent(reply)) {
85 //logger.error("FTP move file error. code:" + reply);
86 System.out.println("FTP move file error. code:" + reply);
87 return false;
88 }
89 reply = client.sendCommand(FTPCmd.RNTO, targetFileName);
90 if (FTPReply.isNegativePermanent(reply)) {
91 //logger.error("FTP move file error. code:" + reply);
92 System.out.println("FTP move file error. code:" + reply);
93 return false;
94 }
95 return true;
96 }
97
98 /**
99 * 读取文件列表
100 */
101 public static List<String> getFileNameList(FTPClient client) throws IOException {
102 FTPFile[] files = client.listFiles();
103 List<String> fileNameList = new ArrayList<>();
104 for (FTPFile file : files) {
105 if (file.isFile()) {
106 fileNameList.add(file.getName());
107 }
108 }
109 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
110 throw new IOException("get file name list error");
111 }
112 return fileNameList;
113 }
114
115 /**
116 * 读文件
117 */
118 public static String readFile(String path, FTPClient client) throws IOException {
119 client.setFileType(FTP.EBCDIC_FILE_TYPE);
120 InputStream is = client.retrieveFileStream(path);
121 if (is == null) {
122 return null;
123 }
124 BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));
125 StringBuilder sb = new StringBuilder();
126 String str;
127 while ((str = bf.readLine()) != null) {
128 sb.append(str).append("\n");
129 }
130 bf.close();
131 client.completePendingCommand();
132 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
133 throw new IOException("Remote file net closed success");
134 }
135 return sb.toString();
136 }
137
138 @Deprecated
139 static boolean downFile(String remotePath, String localPath, FTPClient client)
140 throws IOException {
141 FileOutputStream fos = new FileOutputStream(localPath);
142 client.setFileType(FTPClient.BINARY_FILE_TYPE);
143 client.retrieveFile(remotePath, fos);
144 client.completePendingCommand();
145 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
146 throw new IOException("Remote file net closed success");
147 }
148 return false;
149 }
150
151 /**
152 * 写文件
153 */
154 public static boolean storeAsFile(String context, String remotePath, FTPClient client)
155 throws IOException {
156 OutputStream out = client.storeFileStream(remotePath);
157 OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
158 writer.write(context);
159 writer.flush();
160 writer.close();
161 return true;
162 }
163
164 public static void close(FTPClient client) {
165 try {
166 if (client != null) {
167 client.disconnect();
168 }
169 } catch (IOException e) {
170
171 }
172 }
173 }
FTPUtil
以上是 [Java] 使用 Apache的 Commons-net库 实现FTP操作 的全部内容, 来源链接: utcz.com/z/392496.html