package com.ailk.qw.util;

import it.sauronsoftware.ftp4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.File;
import java.io.IOException; public class FtpUtil {
final static Logger log = LoggerFactory.getLogger(FtpUtil.class); public static FTPClient getFtp(String ip, int port, String username, String password, String charSet) throws FTPException, IOException, FTPIllegalReplyException {
FTPClient client = new FTPClient();
client.connect(ip, port);
client.login(username, password);
client.setCharset(charSet);
client.setType(FTPClient.TYPE_BINARY);//二进制
return client;
} /**
* 远程移动文件
*
* @param ip
* @param port
* @param username
* @param password
* @param fromPath
* @param fromFileName
* @param toPath
* @param toFileName
* @return
*/
public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName) {
boolean result = false;
FTPClient client = null;
log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
try {
client = getFtp(ip, port, username, password, "UTF-8");
//client.changeDirectory(fromPath);
client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (IOException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
return result;
} } /**
* 远程移动文件
*
* @param ip
* @param port
* @param username
* @param password
* @param fromPath
* @param fromFileName
* @param toPath
* @param toFileName
* @param charSet
* @return
*/
public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName, String charSet) {
boolean result = false;
FTPClient client = null;
log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
try {
client = getFtp(ip, port, username, password, charSet);
//client.changeDirectory(fromPath);
client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (IOException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
return result;
} } /**
* 下载文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param remoteFileName
* @param localPath
* @param localFileName
* @return
*/
public static boolean download(String ip, int port, String username, String password, String remotePath, String remoteFileName, String localPath, String localFileName) {
boolean result = false;
FTPClient client = null;
log.info("开始从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName);
try {
client = getFtp(ip, port, username, password, "UTF-8");
client.changeDirectory(remotePath);
client.download(remoteFileName, new File(localPath + File.separatorChar + localFileName));
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (IOException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPAbortedException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPDataTransferException e) {
log.info("从" + ip + "服务器传目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "结果为:" + result);
return result;
}
} /**
* 上传文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param file
* @return
*/
public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName) {
FTPClient client = null;
boolean result = false;
try {
log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
client = getFtp(ip, port, username, password, "UTF-8");
client.changeDirectory(remotePath);
client.changeDirectoryUp();
client.changeDirectory("temp");
String fromPath = client.currentDirectory();
String fromFileName = oldFileName;
client.upload(file);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName);
} catch (FTPException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (IOException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPAbortedException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPDataTransferException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
return result;
}
} /**
* 上传文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param file
* @param charSet
* @return
*/
public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName, String charSet) {
FTPClient client = null;
boolean result = false;
try {
log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
client = getFtp(ip, port, username, password, charSet);
client.changeDirectory(remotePath);
client.changeDirectoryUp();
client.changeDirectory("temp");
String fromPath = client.currentDirectory();
String fromFileName = oldFileName;
client.upload(file);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName, charSet);
} catch (FTPException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (IOException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPAbortedException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPDataTransferException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
return result;
}
} }

java FTP各种操作的更多相关文章

  1. 用edtftpj实现Java FTP客户端工具

    edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...

  2. 关于Java FTP SFTP的相关实际问题

    第一个: java ftp使用的是Apache common-net,但是FTP服务侧提供的FTP服务器只支持SFTP,结果报 java.net.ConnectException: Connectio ...

  3. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  4. 使用JWPL (Java Wikipedia Library)操作维基百科数据

    使用JWPL (Java Wikipedia Library)操作维基百科数据 1. JWPL介绍 JWPL(Java Wikipedia Library)是一个开源的访问wikipeida数据的Ja ...

  5. Java FTP 基本操作

    最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容. FTP基础 关于 FTP 基础推荐阅读<使用 Socket 通信实现 FTP 客户端程序>,其中需要特别注意的是主动模 ...

  6. Java FTP客户端开源类库 edtFTPj

    edtFTPj/Free是免费的流行的Java FTP库,全球公司依靠edtFTPj /Free 为它们的Java应用程序添加FTP客户端功能. (收费的支持SFTP.FTPS的edtFTPj/PRO ...

  7. Java Spring mvc 操作 Redis 及 Redis 集群

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...

  8. Java的JDBC操作

    Java的JDBC操作 [TOC] 1.JDBC入门 1.1.什么是JDBC JDBC从物理结构上来说就是java语言访问数据库的一套接口集合,本质上是java语言根数据库之间的协议.JDBC提供一组 ...

  9. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

随机推荐

  1. 《深入理解Java虚拟机》(四)虚拟机性能监控与故障处理工具

    虚拟机性能监控与故障处理工具 详解 4.1 概述 本文参考的是周志明的 <深入理解Java虚拟机> 第四章 ,为了整理思路,简单记录一下,方便后期查阅. JDK本身提供了很多方便的JVM性 ...

  2. Python爬虫之网页图片抓取

    一.引入 这段时间一直在学习Python的东西,以前就听说Python爬虫多厉害,正好现在学到这里,跟着小甲鱼的Python视频写了一个爬虫程序,能实现简单的网页图片下载. 二.代码 __author ...

  3. [PHP] 算法-合并两个有序链表为一个有序链表的PHP实现

    合并两个有序的链表为一个有序的链表: 类似归并排序中合并两个数组的部分 1.遍历链表1和链表2,比较链表1和2中的元素大小 2.如果链表1结点大于链表2的结点,该结点放入第三方链表 3.链表1往下走一 ...

  4. 【 js 工具 】如何使用Git上传本地项目到github?(mac版)

    在此假设你已经在 github 上创建好了一个项目,像这样: 并且你已经完成了自己的项目代码, 同时你也已经安装了 git,然后 let's start. 首先,建一个文件夹比如文中演示的是 微信小程 ...

  5. linux学习笔记-解决google-chrome打开后弹出输入密码以解锁您的登录密钥环的提示

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.理论知识 1.密钥的作用 google-chrome存储了网站登录时使用的账号和密码信息,这个密钥是用来保护这些信息的 2. ...

  6. DOM事件-调用函数

    通过调用函数改变其内容: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> ...

  7. 【代码笔记】Web-HTML-CSS

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. AWT初步—Frame和 Panel

    初识 AWT       GUI 和 AWT GUI:Graphics User Interface  图形用户界面 AWT:Abstract Window Toolkit  抽象窗口工具集 之前的程 ...

  9. python 提取linux的硬件信息

    1.代码实现目的 注:代码可以直接粘贴使用 为了实现对主机的整体有明确的了解,实现了下面的代码功能 代码主要功能是: 提取到主机的内存.硬盘.CPU.型号等信息 插入到mysql的数据库中 2.代码具 ...

  10. const修饰指针的三种效果

    当用const进行修饰时,根据const位置的不同有三种不同效果. 判断的标准是:const修饰谁,谁的内容就是不可变的. 1 const int *p = &a; const修饰*p, *p ...