java使用ftp,

  java使用ftp,

  00-1010前言windows Server Build FTP服务工具类方法代码显示使用示例

  10月10-1010,网上找了很多FTP java工具类,发现文章代码又长又臃肿。即使找到写的代码也还可以,封装的常用操作方法也不全面,所以我花了半天时间实现了一个好用的工具类。一开始我想用java自带的ftpClient的jar来封装。后来对比了apache的jar工具包,发现远没有apache的好用,于是决定用apache的ftp的jar来封装FTP操作类。

  00-1010打开控制页面,以win 10为例。

  点击程序

  选择或关闭Windows功能。

  检查Internet信息服务、IIS管理控制平台和万维网服务下的FTP相关服务,然后单击确定。

  打开IIS管理器。

  选择网站,鼠标右键,添加FTP站点。

  添加网站名称,选择本地物理路径,设置完成后点击。

  填写您的intranet ip,选择No SSL,然后单击Next。

  勾选匿名(访问时不需要验证账号密码),允许所有用户,选择读写权限(根据自己的需要),点击完成。

  将自己设置的ip和端口ftp://ip:port输入到同一内网任意一台电脑的文件夹中,即可访问。

  00-1010无账号密码登录方式无账号密码登录方式字符转码方式判断文件目录是否存在方式获取文件列表方式上传文件方式下载文件方式上传文件夹方式下载文件夹方式删除文件方式删除文件夹方式创建文件夹方式重命名文件方式

  

目录

pom文件引入了依赖关系commons-net jar

 

  !-https://mvn repository.com/artifact/commons-net/commons-net-dependency groupid commons-net/groupid artifact commons-net/artifact id version 3.6/version/dependency工具类完整代码

  导入org . Apache.commons . net . FTP . *;导入Java . io . *;导入Java . util . ArrayList;导入Java . util . list;/** * Java FTP工具类*/public类FTP util { private static FTP client FTP;/* * *方法描述3360转码*/私有静态字符串转码(string text){ try { return new string(text . getbytes( gbk ),FTP . default _ control _ encoding);} catch(UnsupportedEncodingException e){ return null;}}/* * *方法描述3360 Connect ftp Server匿名登录无密码*/Public Static Void Connect Server(String IP,Intport)抛出IO异常{ConnectServer (IP,port,匿名,null);}/* * *方法描述3360连接到ftp服务器*/公共静态void连接

  ectServer(String ip, int port, String user, String password) throws IOException { // 连接ftp服务器 ftp = new FTPClient(); ftp.connect(ip, port); // 登录ftp服务器 ftp.login(user, password); //设置编码 ftp.setControlEncoding("GBK"); //设置文件类型 ftp.setFileType(FTP.BINARY_FILE_TYPE); } /** * 关闭连接 */ public static void closeServer() throws IOException { if (ftp.isConnected()) { ftp.logout(); ftp.disconnect(); } } /** * 判断目录是否存在 */ public static boolean existDirectory(String pathname) throws IOException { boolean flag = false; FTPFile[] ftpFileArr = ftp.listFiles(pathname); for (FTPFile ftpFile : ftpFileArr) { if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(pathname)) { flag = true; break; } } return flag; } /* * 获取文件列表 */ public static List<String> listFiles(String path) throws IOException { FTPFile[] ftpFiles = ftp.listFiles(path); List<String> retList = new ArrayList<String>(); for (FTPFile ftpFile : ftpFiles) { retList.add(ftpFile.getName()); } return retList; } /** * 上传文件 */ public static boolean uploadFile(String remote,String local) throws IOException { InputStream is=new FileInputStream(local); return ftp.storeFile(transcode(remote),is); } /** * 下载文件 */ public static boolean downloadFile(String remote,String local) throws IOException { OutputStream out=new FileOutputStream(local); return ftp.retrieveFile(transcode(remote),out); } /** * 删除文件 */ public static boolean deleteFile(String remote) throws IOException { return ftp.deleteFile(transcode(remote)); } /** * 删除文件夹 */ public static void deleteFolder(String remote) throws IOException { FTPFile[] ftpFiles=ftp.listFiles(transcode(remote)); for (FTPFile ftpFile : ftpFiles) { if(ftpFile.isDirectory()){ deleteFolder(remote+"/"+ftpFile.getName()); ftp.removeDirectory(transcode(remote+"/"+ftpFile.getName())); }else{ deleteFile(ftpFile.getName()); } } ftp.removeDirectory(transcode(remote)); } /** * 上传文件夹到ftp服务器 */ public static void uploadFolder(String remote,String local) throws IOException { File localFile=new File(local); if(localFile.isDirectory()){ String remoteDir=remote+"/"+localFile.getName(); makeDirectory(remoteDir); File[] partFiles=localFile.listFiles(); for (File file : partFiles) { if(file.isDirectory()){ uploadFolder(remoteDir+"/"+file.getName(),local+"/"+file.getName()); }else { uploadFile(remoteDir+"/"+file.getName(),local+"/"+file.getName()); } } } } /** * 下载文件夹到本地 */ public static void downloadFolder(String remote,String local) throws IOException { File localFile=new File(local); if(!localFile.exists()){ localFile.mkdirs(); } FTPFile[] ftpFiles=ftp.listFiles(transcode(remote)); for (FTPFile ftpFile : ftpFiles) { if(ftpFile.isDirectory()){ downloadFolder(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName()); }else { downloadFile(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName()); } } } /** * 创建文件夹 */ public static void makeDirectory(String remote) throws IOException { if(remote.startsWith("/")){ remote=remote.substring(1); } String[] dirNames = remote.split("/"); String tempPath=""; for (String dirName : dirNames) { tempPath=tempPath+"/"+dirName; ftp.makeDirectory(transcode(tempPath)); } } /** * 重命名 */ public static boolean rename(String from, String to) throws IOException { return ftp.rename(transcode(from),transcode(to)); } }

 

  

使用示例

 public static void main(String[] args) throws IOException { //匿名免密码登录 FTPUtil.connectServer("172.16.10.201",19001,"anonymous",null); //下载ftp根目录所有文件到本地文件夹 FTPUtil.downloadFolder("/","D://ftp"); //删除文件夹以及文件 FTPUtil.deleteFolder("tarzan"); //创建文件夹 FTPUtil.makeDirectory("tarzan/cms"); //文件夹或文件重命名 FTPUtil.rename("泰山","泰山123"); //上传文件夹 FTPUtil.uploadFolder("software","D:\Git"); }

到此这篇关于基于Java手写一个好用的FTP操作工具类的文章就介绍到这了,更多相关Java FTP操作工具类内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: