java FTP各種操作
阿新 • • 發佈:2018-05-30
移動 last new nco subst user error rec ret
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各種操作