java FTP各种操作

Posted 小宇1994

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}

// public static void main(String args[]) throws UnsupportedEncodingException {
// String ip = "10.248.12.35";
// int port = 21;
// String username = "sm7";
// String password = "sm7";
// String localpath = "/ainmc/work/cpr_att/bak";
// String fileName = "APP_270_EVT_60940_CPRP_平台共管_1个对象告警.xls";
// String remotePath = "/ainmc/work/cpr_att/";
// String remoteFileName = "APP_270_EVT_60940_CPRP_1.xls";
// FtpUtil.remoteMove(ip,21,username,password,localpath,fileName,remotePath,remoteFileName,"GBK");
// FtpUtil.remoteMove(ip,21,username,password,remotePath,remoteFileName,remotePath,fileName,"UTF-8");
// }

public static void main2(String args[]){
String fileName="APP_270_EVT_60940_CPRP_平台共管_1个对象告警.xls";
String name=fileName.substring(0,17)+fileName.substring(fileName.lastIndexOf("."));
System.out.println(name);
}

public static void main(String args[]) {
FTPClient client = null;
try {
client=FtpUtil.getFtp("10.248.12.31",21,"sm7","sm7","gb2312");
client.changeDirectory("/interface/file/unknown");
FTPFile[] list=client.list("APP_100*");
for(FTPFile file:list){
System.out.println(file.getName());
}
} catch (FTPException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FTPIllegalReplyException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FTPAbortedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FTPListParseException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FTPDataTransferException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

}

}













































































































































































































































































































































以上是关于java FTP各种操作的主要内容,如果未能解决你的问题,请参考以下文章

2.5 定义FTP工具的各种方法

盘点用Java抓取HTTP服务器和FTP服务器的网页数据或图片等数据的实用技巧

利用PBFunc在Powerbuilder中进行FTP操作

2017.4.11上午

FTP服务简介

怎么用Java实现FTP上传