码迷,mamicode.com
首页 > 编程语言 > 详细

java FTP各种操作

时间:2018-05-30 15:06:49      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:MF   对象   nsf   bst   目录文件   parse   substr   throws   soft   

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各种操作

标签:MF   对象   nsf   bst   目录文件   parse   substr   throws   soft   

原文地址:https://www.cnblogs.com/xiaoyu1994/p/9110803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!