设为首页 加入收藏

TOP

FTPClientUtil FTP客户端工具(二)
2015-07-20 17:58:28 来源: 作者: 【 】 浏览:8
Tags:FTPClientUtil FTP 客户端 工具
l.set(ftpClient); } } /** * @description \u6279\u91CF\u5220\u9664\u6240\u6709\u76EE\u5F55\u4E0B\u7684\u5BF9\u5E94\u7684\u6587\u4EF6 * @param delFiles * @return * @throws FTPClientException * @throws IOException */ public static boolean deleteRemoteFiles(String[] delFiles) throws FTPClientException, IOException { List list=listNames();//\u83B7\u53D6\u6240\u6709\u7684\u6587\u4EF6\u540D for(String filename:delFiles){ for(Iterator it=list.iterator();it.hasNext();){ String filepath=it.next(); if(filepath.contains(filename)){//\u5982\u679C\u8BE5\u8DEF\u5F84\u5305\u542B\u8BE5\u6587\u4EF6\u540D\u5219\u5220\u9664 boolean result= getFTPClient().deleteFile(filepath); if(!result){ return result; } } } } return true; } /** * @description \u5217\u51FA\u8FDC\u7A0B\u9ED8\u8BA4\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6 * @return \u8FDC\u7A0B\u9ED8\u8BA4\u76EE\u5F55\u4E0B\u6240\u6709\u6587\u4EF6\u540D\u7684\u5217\u8868\uFF0C\u76EE\u5F55\u4E0D\u5B58\u5728\u6216\u8005\u76EE\u5F55\u4E0B\u6CA1\u6709\u6587\u4EF6\u65F6\u8FD4\u56DE0\u957F\u5EA6\u7684\u6570\u7EC4 * @throws FTPClientException * @throws IOException */ public static List listNames() throws FTPClientException, IOException { return listNames(null); } public static List listNames(String remotePath) throws FTPClientException, IOException { return listNames(remotePath, true); } /** * @description\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6 * @param remotePath * \u8FDC\u7A0B\u76EE\u5F55\u540D * @param autoClose * \u662F\u5426\u81EA\u52A8\u5173\u95ED\u5F53\u524D\u8FDE\u63A5 * @return \u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u6587\u4EF6\u540D\u7684\u5217\u8868\uFF0C\u76EE\u5F55\u4E0D\u5B58\u5728\u6216\u8005\u76EE\u5F55\u4E0B\u6CA1\u6709\u6587\u4EF6\u65F6\u8FD4\u56DE0\u957F\u5EA6\u7684\u6570\u7EC4 * @throws FTPClientException * @throws IOException */ public static List listNames(String remotePath, boolean containSubdirectory) throws FTPClientException, IOException { if (null == remotePath) { remotePath = "." + File.separator; } try { FTPFile[] files = getFTPClient().listFiles(remotePath); if (files.length < 3) { return listFileNames; } for (FTPFile file : files) { if (!file.getName().equals(".") && !file.getName().equals("..")) { if (file.isFile()) {// \u6587\u4EF6 listFileNames .add("." + File.separator + file.getName()); } else {// \u76EE\u5F55 listNames2( remotePath + file.getName() + File.separator, containSubdirectory); } } } } catch (IOException e) { throw new FTPClientException("\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6\u65F6\u51FA\u73B0\u5F02\u5E38", e); } return listFileNames; } //listNames2\u9012\u5F52\u65B9\u6CD5 private static void listNames2(String remotePath, boolean containSubdirectory) throws FTPClientException { try { FTPClient client = getFTPClient(); client.changeWorkingDirectory(remotePath); FTPFile[] files = client.listFiles(remotePath); if (files.length < 3) { return; } for (FTPFile file : files) { if (!file.equals(".") && !file.equals("..")) { if (file.isFile()) { listFileNames.add(remotePath + file.getName()); } if (file.isDirectory() && (!".".equals(file.getName())) && (!"..".equals(file.getName()))) { String path = remotePath + file.getName() + File.separator; listNames2(path, containSubdirectory); } } } } catch (IOException e) { throw new FTPClientException("\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6\u65F6\u51FA\u73B0\u5F02\u5E38", e); } } /** * * @param remotePath * \u8FDC\u7A0B\u8DEF\u5F84 * @param fileName * \u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u540D * @param localInputStream * \u672C\u5730InputStream\u6D41 * @return * @throws IOException * @throws FTPClientException */ public static boolean uploadToRemote(String remotePath, String fileName, InputStream localInputStream) throws IOException, FTPClientException { remotePath=remotePath+File.separator; FTPClient client = getFTPClient(); int reply; reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); } client.makeDirectory(remotePath);//\u5728\u670D\u52A1\u7AEF\u5EFA\u7ACB\u8BE5\u6587\u4EF6\u5939 client.changeWorkingDirectory(remotePath); boolean result = client.storeFile(remotePath+fileName, localInputStream); localInputStream.close(); return result; } /** * * @param remotePath * \u8FDC\u7A0B\u8DEF\u5F84 * @param localPath * \u672C\u5730\u8DEF\u5F84 * @return * @throws IOException * @throws FTPClientException * @throws IOException */ public static boolean downloadToLocal(String remotePath, String localPath) throws IOException, FTPClientException, IOException { return downloadToLocal(remotePath, localPath, null); } /** * * @param remotePath * \u8FDC\u7A0B\u8DEF\u5F84 * @param localPath * \u8981\u4E0B\u8F7D\u7684\u8DEF\u5F84 * @param fileNames * \u6240\u6709\u8981\u4E0B\u8F7D\u7684\u6587\u4EF6\u540D\u5B57 * @return * @throws IOException * @throws FTPClientException * @throws IOException */ public static boolean downloadToLocal(String remotePath, String localPath, String[] fileNames) throws IOException, FTPClientException, IOException { remotePath=remotePath+File.separator; localPath=localPath+File.separator; FTPClient client = getFTPClient(); client.changeWorkingDirectory(remotePath); FTPFile[] ftpList = client.listFiles(remotePath); boolean result = true; if (null == fileNames) { for (FTPFile f : ftpList) { if (f.getSize() > 0) { File file = new File(localPath); file.mkdirs(); OutputStream out = new FileOutputStream(localPath + f.getName()); result = client.retrieveFile(f.getName(), out); // \u4E0B\u8F7D out.close(); if (!result) { break; } } } } else { for (String fileName : fileNames) { File file = new File(localPath); file.mkdirs(); OutputStream out = new FileOutputStream(localPath + File.separator + fileName); result = client.retrieveFile(fileName, out); // \u4E0B\u8F7D out.close(); if (!result) { break; } } } return result; } /** * @param client * @param fileName * \u8FDC\u7A0B\u8DEF\u5F84\u540D * @return * @throws IOException * @throws FTPClientException */ public static int getRemoteFileSize(String fileName) throws IOException, FTPClientException { FTPClient client = getFTPClient(); int size = 0; FTPFile[] ftpList = client.listFiles(); for (FTPFile f : ftpList) { if (f.getName().equalsIgnoreCase(fileName)) { size = (int) f.getSize(); } } return size; } /** * * @param filename * \u8981\u4E0B\u8F7D\u7684\u6587\u4EF6\u540D \u4ECE\u6574\u4E2A\u670D\u52A1\u5668\u4E2D\u67E5\u627E,\u53EF\u80FD\u627E\u5230\u591A\u4E2A\u76F8\u540C\u540D\u5B57\u7684\u6587\u4EF6,\u
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1754 I Hate It (线段树) 下一篇HDU 3065 病毒侵袭持续中 AC自动..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: