[Java] 使用 Apache的 Commons-net库 实现FTP操作
Posted Phantom01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java] 使用 Apache的 Commons-net库 实现FTP操作相关的知识,希望对你有一定的参考价值。
因为最近工作中需要用到FTP操作,而手上又没有现成的FTP代码。就去网上找了一下,发现大家都使用Apache的 Commons-net库中的FTPClient。
但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。
1 /** 2 * 封装了一些FTP操作 3 */ 4 import java.io.BufferedReader; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStream; 9 import java.io.OutputStreamWriter; 10 11 import org.apache.commons.net.ftp.FTP; 12 import org.apache.commons.net.ftp.FTPClient; 13 import org.apache.commons.net.ftp.FTPCmd; 14 import org.apache.commons.net.ftp.FTPReply; 15 import org.apache.log4j.Logger; 16 17 public class FTPUtil { 18 private static Logger logger = Logger.getLogger(FTPUtil.class); 19 20 private static FTPClient getConnection() { 21 FTPClient client = new FTPClient(); 22 client.setDataTimeout(30000); 23 client.setDefaultTimeout(30000); 24 return client; 25 } 26 27 public static FTPClient getConnection(String host) throws IOException { 28 FTPClient client = getConnection(); 29 client.connect(host); 30 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { 31 throw new IOException("connect error"); 32 } 33 return client; 34 } 35 public static FTPClient getConnection(String host, int port) throws IOException { 36 FTPClient client = getConnection(); 37 client.connect(host, port); 38 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { 39 throw new IOException("connect error"); 40 } 41 return client; 42 } 43 44 static FTPClient getConnection(String host, String username, String password) throws IOException { 45 FTPClient client= getConnection(host); 46 client.login(username, password); 47 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { 48 throw new IOException("login error"); 49 } 50 return client; 51 } 52 53 static FTPClient getConnection(String host, int port, String username, String password) 54 throws IOException { 55 FTPClient client = getConnection(host, port); 56 client.login(username, password); 57 if (!FTPReply.isPositiveCompletion(client.getReplyCode())) { 58 throw new IOException("login error"); 59 } 60 return client; 61 } 62 63 /** 64 * 移动文件(若目标文件存在则不移动,并返回false) 65 */ 66 static boolean moveFile(String curFileName, String targetFileName, FTPClient client) 67 throws IOException { 68 int reply; 69 reply = client.sendCommand(FTPCmd.RNFR, curFileName); 70 if (FTPReply.isNegativePermanent(reply)) { 71 //logger.error("FTP move file error. code:" + reply); 72 System.out.println("FTP move file error. code:" + reply); 73 return false; 74 } 75 reply = client.sendCommand(FTPCmd.RNTO, targetFileName); 76 if (FTPReply.isNegativePermanent(reply)) { 77 //logger.error("FTP move file error. code:" + reply); 78 System.out.println("FTP move file error. code:" + reply); 79 return false; 80 } 81 return true; 82 } 83 84 /** 85 * 读取文件列表 86 */ 87 @Deprecated 88 static String[] getFileNameList(FTPClient client) throws IOException { 89 String names[] = client.listNames(); 90 if (!FTPReply.isPositiveCompletion(client.getReply())) { 91 throw new IOException("get file name list error"); 92 } 93 return names; 94 } 95 96 /** 97 * 读文件 98 */ 99 static String readFile(String path, FTPClient client) throws IOException { 100 client.setFileType(FTP.EBCDIC_FILE_TYPE); 101 BufferedReader bf = new BufferedReader( 102 new InputStreamReader(client.retrieveFileStream(path), "UTF-8")); 103 StringBuilder sb = new StringBuilder(); 104 String str; 105 while ((str = bf.readLine()) != null) { 106 sb.append(str).append("\n"); 107 } 108 return sb.toString(); 109 } 110 111 @Deprecated 112 static boolean downFile(String remotePath, String localPath, FTPClient client) 113 throws IOException { 114 FileOutputStream fos = new FileOutputStream(localPath); 115 client.setFileType(FTPClient.BINARY_FILE_TYPE); 116 client.retrieveFile(remotePath, fos); 117 return false; 118 } 119 120 /** 121 * 写文件 122 */ 123 static boolean storeAsFile(String context, String remotePath, FTPClient client) 124 throws IOException { 125 OutputStream out = client.storeFileStream(remotePath); 126 OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8"); 127 writer.write(context); 128 writer.flush(); 129 writer.close(); 130 return true; 131 } 132 133 static void close(FTPClient client) { 134 try { 135 if (client != null) { 136 client.disconnect(); 137 } 138 } catch (IOException e) { 139 140 } 141 } 142 }
以上是关于[Java] 使用 Apache的 Commons-net库 实现FTP操作的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
java: 包 org.apache.commons.math3.fraction 不存在
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException