清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
apache commons-net可以很方便的实现,但是这个第三方包中对文件夹的删除与创建(级联)操作并不是特别的方便。删除文件夹必须保证该文件夹下没有任何 文件,创建文件夹也必须要求父文件夹存在。为了方便以后使用方便,对其进行了简单的封装,主要针对删除与文件夹创建。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | package cn.androiddevelop.io; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.SocketException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; /** * ftp客户端 * * @author Yuedong Li * */ public class FtpClient { private FTPClient client; public FtpClient(String host, String userName, String password) throws SocketException, IOException { initFtpClient(host, 21 , userName, password); } public FtpClient(String host, int port, String userName, String password) throws SocketException, IOException { initFtpClient(host, port, userName, password); } /** * 登录 * * @param host * @param port * @param userName * @param password * @throws SocketException * @throws IOException */ public void initFtpClient(String host, int port, String userName, String password) throws SocketException, IOException { client = new FTPClient(); client.connect(host, port); client.login(userName, password); } /** * 得到所有目录 * * @param remotePath * @return * @throws IOException */ public FTPFile[] listFiles(String remotePath) throws IOException { if (client == null ) return null ; client.changeWorkingDirectory(remotePath); return client.listFiles(); } /** * 上传 * * @param localPath * 本地路径 * @param remotePath * ftp路径 * @return 上传是否成功 * @throws IOException */ public boolean upload(String localPath, String remotePath) throws IOException { if (client == null ) return false ; boolean res = false ; FileInputStream fileInputStream = new FileInputStream(localPath); int index = remotePath.lastIndexOf( '/' ); if (index != - 1 ) { client.setFileType(FTP.BINARY_FILE_TYPE); client.changeWorkingDirectory(remotePath.substring( 0 , index)); res = client.storeFile(remotePath.substring(index + 1 ), fileInputStream); } fileInputStream.close(); return res; } /** * 下载 * * @param remotePath * ftp路径 * @param localPath * 本地路径 * @return 下载是否成功 * @throws IOException */ public boolean download(String remotePath, String localPath) throws IOException { if (client == null ) return false ; boolean res = false ; FileOutputStream fileOutputStream = new FileOutputStream(localPath); res = client.retrieveFile(remotePath, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); return res; } /** * 删除文件 * * @param remotePath ftp端路径 * @return * @throws IOException */ public boolean delete(String remotePath) throws IOException { if (client == null ) return false ; return client.deleteFile(remotePath) || deleteDirectory(remotePath); } /** * 创建目录 * * @param remotePath * @throws IOException */ public boolean makeDirectory(String remotePath) throws IOException { if (client == null ) return false ; String[] item = remotePath.split( "/" ); String currentPath = "" ; for ( int i = 0 ; i < item.length - 1 ; i++) { currentPath = currentPath + "/" + item[i]; client.makeDirectory(currentPath); } return client.makeDirectory(remotePath); } /** * 删除文件 * * @param remotePath ftp端路径 * @return * @throws IOException */ private boolean deleteDirectory(String remotePath) throws IOException { FTPFile[] files = listFiles(remotePath); for ( int i = 0 ; i < files.length; i++) { if (files[i].isDirectory()) { deleteDirectory(remotePath + "/" + files[i].getName()); } else { client.deleteFile(remotePath + "/" + files[i].getName()); } } return client.removeDirectory(remotePath); } /** * 重命名 * * @param remoteOldPath * @param remoteNewPath * @return * @throws IOException */ public boolean rename(String remoteOldPath, String remoteNewPath) throws IOException { if (client == null ) return false ; return client.rename(remoteOldPath, remoteNewPath); } /** * 退出登录 * * @throws IOException */ public void close() throws IOException { if (client != null ) client.logout(); } } |