java apache commons net_[Java] 使用 Apache的 Commons-net库 实现FTP操作

1 packagecom.best.oasis.util.helper;2

3 /**

4 * 封装了一些FTP操作5 * Created by bl05973 on 2016/3/11.6 */

7

8 importjava.io.BufferedReader;9 importjava.io.FileOutputStream;10 importjava.io.IOException;11 importjava.io.InputStream;12 importjava.io.InputStreamReader;13 importjava.io.OutputStream;14 importjava.io.OutputStreamWriter;15 importjava.util.ArrayList;16 importjava.util.List;17

18 importorg.apache.commons.net.ftp.FTP;19 importorg.apache.commons.net.ftp.FTPClient;20 importorg.apache.commons.net.ftp.FTPCmd;21 importorg.apache.commons.net.ftp.FTPFile;22 importorg.apache.commons.net.ftp.FTPReply;23 importorg.apache.log4j.Logger;24

25 public classFTPUtil {26 private static Logger logger = Logger.getLogger(FTPUtil.class);27

28 private staticFTPClient getConnection() {29 FTPClient client = newFTPClient();30 client.setControlEncoding("UTF-8");31 client.setDataTimeout(30000);32 client.setDefaultTimeout(30000);33 returnclient;34 }35

36 public static FTPClient getConnection(String host) throwsIOException {37 FTPClient client =getConnection();38 client.connect(host);39 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {40 throw new IOException("connect error");41 }42 returnclient;43 }44 public static FTPClient getConnection(String host, int port) throwsIOException {45 FTPClient client =getConnection();46 client.connect(host, port);47 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {48 throw new IOException("connect error");49 }50 returnclient;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 returnclient;63 }64

65 public static FTPClient getConnection(String host, intport, String username, String password)66 throwsIOException {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 returnclient;75 }76

77 /**

78 * 移动文件(若目标文件存在则不移动,并返回false)79 */

80 public static booleanmoveFile(String curFileName, String targetFileName, FTPClient client)81 throwsIOException {82 intreply;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 getFileNameList(FTPClient client) throwsIOException {102 FTPFile[] files =client.listFiles();103 List 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 returnfileNameList;113 }114

115 /**

116 * 读文件117 */

118 public static String readFile(String path, FTPClient client) throwsIOException {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 = newStringBuilder();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 returnsb.toString();136 }137

138 @Deprecated139 static booleandownFile(String remotePath, String localPath, FTPClient client)140 throwsIOException {141 FileOutputStream fos = newFileOutputStream(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 booleanstoreAsFile(String context, String remotePath, FTPClient client)155 throwsIOException {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 voidclose(FTPClient client) {165 try{166 if (client != null) {167 client.disconnect();168 }169 } catch(IOException e) {170

171 }172 }173 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值