java FTP各种操作
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各种操作的更多相关文章
- 用edtftpj实现Java FTP客户端工具
edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...
- 关于Java FTP SFTP的相关实际问题
第一个: java ftp使用的是Apache common-net,但是FTP服务侧提供的FTP服务器只支持SFTP,结果报 java.net.ConnectException: Connectio ...
- Java ftp 上传文件和下载文件
今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...
- 使用JWPL (Java Wikipedia Library)操作维基百科数据
使用JWPL (Java Wikipedia Library)操作维基百科数据 1. JWPL介绍 JWPL(Java Wikipedia Library)是一个开源的访问wikipeida数据的Ja ...
- Java FTP 基本操作
最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容. FTP基础 关于 FTP 基础推荐阅读<使用 Socket 通信实现 FTP 客户端程序>,其中需要特别注意的是主动模 ...
- Java FTP客户端开源类库 edtFTPj
edtFTPj/Free是免费的流行的Java FTP库,全球公司依靠edtFTPj /Free 为它们的Java应用程序添加FTP客户端功能. (收费的支持SFTP.FTPS的edtFTPj/PRO ...
- Java Spring mvc 操作 Redis 及 Redis 集群
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...
- Java的JDBC操作
Java的JDBC操作 [TOC] 1.JDBC入门 1.1.什么是JDBC JDBC从物理结构上来说就是java语言访问数据库的一套接口集合,本质上是java语言根数据库之间的协议.JDBC提供一组 ...
- Java读写文本文件操作
package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...
随机推荐
- babel-polyfill的引用和使用
前两天一个首页项目,想用vue玩耍一下,就用vue-cli搭建了一套vue的开发框架 完成开发.联调和上线后,问题来了 chrome.ff浏览器下都能正常显示的页面,在百度浏览器下愣就显示不出来了 我 ...
- 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组
学C语言,一定绕不过指针这一大难关,而指针最让人头疼的就是各种指向关系,一阶的指针还比较容易掌握,但一旦阶数一高,就很容易理不清楚其中的指向关系,现在我将通过杨辉三角为例,我会用四种方法从内存的角度简 ...
- Java学习笔记之——常用快捷键(eclipse)
* Ctrl+C 复制 * Ctrl+V 粘贴 * Ctrl+A 全选 * Ctrl+S 保存 * Ctrl+Z 撤销 * Ctrl+Y 还原 * Ctrl+X 剪切 * Ctrl+F 查找 * ...
- log4j.appender.file.DatePattern
DailyRollingFileAppender是日志记录软件包Log4J中的一个Appender,它能够按一定的频度滚动日志记录文件. 我们可以按下面的方式配置DailyRollingFileApp ...
- LintCode Binary Search
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- JS中数组去重的九方法
数组去重方法 方法一:运用set结构特点:存储的数据没有重复的,结果为对象,再用Array.from()转换成数组 var arr = [1,1,2,1,3,4,5] ...
- NFS共享权限挂载
mount -t nfs 192.168.2.203:/data/lys /lys -o proto=tcp -o nolock mount 172.16.2.18:/home/arcgisserve ...
- loadrunner 运行脚本-Run-time Settings之Preferences设置
运行脚本-Run-time Settings之Preferences设置 by:授客 QQ:1033553122 打开Preferences设置对话框,这里提供了对运行时的参数选择设置 Enable ...
- android 保存图片,及将图片更新到图库
**保存图片 public static File saveImage(Bitmap bmp) { File appDir = new File(Environment.getExternalStor ...
- C# Aspose.Cells方式导入Excel文件
读取Excel 类 我返回的是DataTable 类型 也可以返回DataSet类型 public class XlsFileHelper { public DataTable ImportExcel ...