通过java与ftp服务器进行交互,费话少说代码如下
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.TimeZone; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; import com.sdjxd.commons.AppConfig; public class FileUpLoad { private FTPClient ftpClient;// ftp客户端 private String strIp;// ftp服务器地址 private int intPort;// ftp端口 private String user;// ftp用户 private String password;// ftp密码 private boolean isLogin = false;// 是否已登录 private static Logger logger = Logger.getLogger(FileUpLoad.class); /*************************************************************************** * */ public FileUpLoad() { this.strIp = AppConfig.getConfig("ftpServerIp"); this.intPort = Integer.valueOf(AppConfig.getConfig("ftpServerPort")); this.user = AppConfig.getConfig("ftpUser"); this.password = AppConfig.getConfig("ftpPassword"); this.ftpClient = new FTPClient(); } /** * @return 判断是否登入成功 */ public boolean ftpLogin() { boolean isLogin = false; FTPClientConfig ftpClientConfig = new FTPClientConfig(); ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID()); this.ftpClient.setControlEncoding("GBK"); this.ftpClient.configure(ftpClientConfig); try { if (this.intPort > 0) { this.ftpClient.connect(this.strIp, this.intPort); } else { this.ftpClient.connect(this.strIp); } // FTP服务器连接回答 int reply = this.ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { this.ftpClient.disconnect(); logger.error("登录FTP服务失败!"); return isLogin; } this.ftpClient.login(this.user, this.password); // 设置传输协议 this.ftpClient.enterLocalPassiveMode(); // 以二进制文件的形式上传,解决上传后文件大小改变问题 this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); logger.info("恭喜" + this.user + "成功登陆FTP服务器"); isLogin = true; } catch (Exception e) { e.printStackTrace(); logger.error(this.user + "登录FTP服务失败!" + e.getMessage()); } this.ftpClient.setBufferSize(1024 * 2); this.ftpClient.setDataTimeout(30 * 1000); this.isLogin = isLogin; return isLogin; } /*************************************************************************** * * 退出关闭服务器链接 */ public void ftpLogOut() { if (null != this.ftpClient && this.ftpClient.isConnected()) { try { boolean reuslt = this.ftpClient.logout();// 退出FTP服务器 if (reuslt) { logger.info("成功退出服务器"); } } catch (IOException e) { e.printStackTrace(); logger.warn("退出FTP服务器异常!" + e.getMessage()); } finally { try { this.ftpClient.disconnect();// 关闭FTP服务器的连接 } catch (IOException e) { e.printStackTrace(); logger.warn("关闭FTP服务器的连接异常!"); } } } } /*************************************************************************** * 上传Ftp文件 * * @param localFile * 本地文件 * @param ftpServerDir * 上传服务器路径 - 应该以/结束 */ public boolean uploadFile(File localFile, String ftpServerDir) { // 如果服务没有连接,先连接服务器 if (!this.isLogin) { ftpLogin(); } BufferedInputStream inStream = null; boolean success = false; try { boolean dirExists = this.ftpClient .changeWorkingDirectory(ftpServerDir);// 改变工作路径 //检查是否存在,如果不存在则创建它 if (dirExists) { logger.debug("更改服务器目录:" + ftpServerDir); } else { if (this.ftpClient.makeDirectory(ftpServerDir)) { logger.info(ftpServerDir + "目录创建成功!"); this.ftpClient.changeWorkingDirectory(ftpServerDir);// 改变工作路径 } else { logger.info(ftpServerDir + "服务器目录已存在,或没有创建权限,或其它原因没有创建成功!"); } } inStream = new BufferedInputStream(new FileInputStream(localFile)); logger.info(localFile.getName() + "开始上传....."); success = this.ftpClient.storeFile(localFile.getName(), inStream); if (success == true) { logger.info(localFile.getName() + "上传成功"); return success; } } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(localFile + "未找到"); } catch (IOException e) { e.printStackTrace(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return success; } /*************************************************************************** * 下载文件 * * @param remoteFileName * 待下载文件名称 * @param localDires * 下载到当地那个路径下 * @param remoteDownLoadPath * remoteFileName所在的路径 */ public boolean downloadFile(String remoteFileName, String localDires, String remoteDownLoadPath) { // 如果服务没有连接,先连接服务器 if (!this.isLogin) { ftpLogin(); } String strFilePath = localDires + remoteFileName; BufferedOutputStream outStream = null; boolean success = false; try { this.ftpClient.changeWorkingDirectory(remoteDownLoadPath); outStream = new BufferedOutputStream(new FileOutputStream( strFilePath)); logger.info(remoteFileName + "开始下载...."); success = this.ftpClient.retrieveFile(remoteFileName, outStream); if (success == true) { logger.info(remoteFileName + "成功下载到" + strFilePath); return success; } } catch (Exception e) { e.printStackTrace(); logger.error(remoteFileName + "下载失败"); } finally { if (null != outStream) { try { outStream.flush(); outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } if (success == false) { logger.error(remoteFileName + "下载失败!!!"); } return success; } }
相关推荐
java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持
Java操作FTP服务器,连接FTP,上传文件,删除文件,下载文件
java操作FTP各种例子,java调用ftp进行远程crud
java操作FTP.pdf
JAVA操作FTP文件服务器上传与下载文件,包含远程动态创建文件夹、查看文件是否存在、删除文件、上传文件和下载文件等方法
编程语言java操作FTP.pdf
java操作FTP[借鉴].pdf
java通过ftp远程下载文件,已经自己使用,稳定,实现了文件夹递归下载。
本类提供了java对ftp的各种操作的!经过精心的整理!相信对初学者有很大的帮助!
里面是我写的用java操作ftp的例子 , 源代码工程包 全齐。希望对阁下有用
一个简单的demo,适合初学者,java语言使用第三方API,进程FTP上传下载的demo
本示例简单的实现了一个用Java代码来访问FTP,根据指定FTP的目录,访问这个路径下面的Ftp的文件,取出这个目录下面所有文件的文件名保存到一个Map中,最后根据系统时间和文件生成时间做对比,得出该日是否正确生成...
这是一个完整的工程,包含jar包,搭上就能用。
主要介绍了java操作ftp下载文件的示例,需要的朋友可以参考下
开发FTP工具包 目前最新的工具,可实现所有FTP操作的功能
②快速知道如何使用java操作ftp文件,避免踩坑,多种操作方式。③ftp文件到底可如何操作。如何获取,如何上传。 阅读建议:此资源以让工作中的同学快速学习使用,直接复制代码即可直接使用,不需要太多改造,当然也...
这是用java实现ftp操作的一些代码,里面有上传,下载,删除服务器上制定文件以及断点续传的操作!!希望能帮助需要的人!!
java 连接 FTP 文件操作(上传,下载,删除,复制
javaFTP目录、文件操作。