Java 使用apachecamel测试文件的存在性
我想使用ApacheCamel测试远程SFTP服务器上是否存在给定的文件。 如果存在,则该方法必须返回true;如果不存在,则返回false;如果存在,则返回异常 客户端无法连接(超时、用户名/密码错误等) 事实上,测试我是否可以登录到服务器并不是一个大问题 测试给定文件是否存在。只需向SFTP服务器发送请求,Java 使用apachecamel测试文件的存在性,java,apache-camel,camel-ftp,Java,Apache Camel,Camel Ftp,我想使用ApacheCamel测试远程SFTP服务器上是否存在给定的文件。 如果存在,则该方法必须返回true;如果不存在,则返回false;如果存在,则返回异常 客户端无法连接(超时、用户名/密码错误等) 事实上,测试我是否可以登录到服务器并不是一个大问题 测试给定文件是否存在。只需向SFTP服务器发送请求,download=false和noop=true 这就是我们必须要做的。一个空文件将下载到我的TMP目录 如果文件存在,则不会下载任何文件。如果我无法登录到服务器,将引发自己的异常 (Ca
download=false
和noop=true
这就是我们必须要做的。一个空文件将下载到我的TMP目录
如果文件存在,则不会下载任何文件。如果我无法登录到服务器,将引发自己的异常
(CannotConnectException
)。
我的观点是,我无法在我的客户机上捕获此异常。
我很确定我做错了什么,但我不知道是什么。
我想在返回的文件中设置一些错误值,以防我无法连接,但是
我觉得这是个“黑客”。有什么方法可以捕获引发的异常吗?如果没有,最好的方法是什么
如果我无法连接,是否为我的客户端获取异常
我的代码如下:
public final class MyFtpClient {
private static final Logger LOG = LoggerFactory.getLogger(MyFtpClient.class);
// Client settings
static String localPath = File.separator + "tmp";
public enum Existence{
EXIST, NOT_EXIST, UNKNOWN
}
private MyFtpClient() {
}
private static Existence doesFileExistAtServer(Main main, String protocol, String user, String password,
String server, int port, String filePathAtServer, String filenameAtServer, String localPath)
{
boolean fileExist = false;
try {
main.addRouteBuilder(new MyFtpClientRouteBuilder(protocol, user, password, server, port, filePathAtServer,
filenameAtServer, localPath));
main.start();
Thread.sleep(5000);
main.stop();
String filePath = localPath + File.separator + filenameAtServer;
File f = new File(filePath);
fileExist = f.exists();
if (fileExist) {
f.delete(); // Just delete it.
return Existence.EXIST;
} else {
return Existence.NOT_EXIST;
}
// I CANNOT CATCH THIS EXCEPTION
} catch (Exception e) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Cannot Connect to the Sftp Server " + server);
LOG.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Cannot Connect to the Sftp Server " + server);
LOG.info(e.getMessage());
return Existence.UNKNOWN;
}
}
public static void main(String[] args) throws Exception {
String protocol = "sftp";
int port = 22;
String server;
String user;
String password;
String filePathAtServer;
String filenameAtServer;
boolean fileExist;
Main main = new Main();
server = "unknown.com";
user = "demo";
password = "password";
filePathAtServer = "/";
filenameAtServer = "readme.txt";
doesFileExistAtServer(main, protocol, user, password, server, port, filePathAtServer, filenameAtServer, localPath);
LOG.info("\nThe accesibility of the file 1 " + filenameAtServer + " at the server " + server + " is " + fileExist + "\n")
}
public class MyFtpClientRouteBuilder extends RouteBuilder {
private static final Logger LOG =
LoggerFactory.getLogger(MyFtpClientRouteBuilder.class);
String protocol = "sftp";
int port = 22;
String server;
String user;
String password;
String filePathAtServer;
String filenameAtServer;
String localPath;
boolean fileExist;
public MyFtpClientRouteBuilder(String protocol, String user, String password, String server, int port,
String filePathAtServer, String filenameAtServer, String localPath) {
super();
this.protocol = protocol;
this.user = user;
this.password = password;
this.server = server;
this.port = port;
this.filePathAtServer = filePathAtServer;
this.filenameAtServer = filenameAtServer;
this.localPath = localPath;
}
private static String generateFromUri(String protocol, String user, String password, String server, int port,
String path, String filename) {
final String downloadFalse = "download=false"; // NO, DO NOT Download the file
final String fastExistsCheck = "fastExistsCheck=true";
final String doNothing = "noop=true"; // Just connect, but DO NOTHING
final String connectFail = "throwExceptionOnConnectFailed=true"; // Just in case a connection fails
final String maxReconnect = "maximumReconnectAttempts=0";
final String bridgeError = "consumer.bridgeErrorHandler=true";
return protocol + "://" + user + "@" + server + ":" + port + path + "?" + "fileName=" + filename + "&"
+ downloadFalse + "&"
+ "password=" + password
+ "&" + fastExistsCheck
+ "&" + connectFail
+ "&" + doNothing
+ "&" + maxReconnect
+ "&" + bridgeError;
}
private static String generateLocalUri(String path) {
final String protocol = "file";
final String allowNullBody = "allowNullBody=true";
final String doNothing = "noop=true";
final String fileExist = "fileExist=move";
final String moveExisting = "moveExisting=oldFile";
return protocol + ":" + path + "?" + allowNullBody + "&" + doNothing + "&" + fileExist + "&" + moveExisting;
}
@Override
public void configure() throws CannotConnectException {
final String fromSftpServer = generateFromUri(protocol, user, password, server, port, filePathAtServer, filenameAtServer);
LOG.info("From: " + fromSftpServer);
final String toLocal = generateLocalUri(localPath);
LOG.info("To: " + toLocal);
onException(GenericFileOperationFailedException.class, JSchException.class)
//.handled(true)
.throwException(new CannotConnectException("Cannot connect to the remote SFTP server " + server))
.log("Cannot connect to the remote SFTP server " + server)
.maximumRedeliveries(0)
.to(toLocal)
.continued(false) // Either handled(true) or continue(false) but NOT both together
.end();
from(fromSftpServer).to(toLocal);
}
}
而我的RouteBuilder如下所示:
public final class MyFtpClient {
private static final Logger LOG = LoggerFactory.getLogger(MyFtpClient.class);
// Client settings
static String localPath = File.separator + "tmp";
public enum Existence{
EXIST, NOT_EXIST, UNKNOWN
}
private MyFtpClient() {
}
private static Existence doesFileExistAtServer(Main main, String protocol, String user, String password,
String server, int port, String filePathAtServer, String filenameAtServer, String localPath)
{
boolean fileExist = false;
try {
main.addRouteBuilder(new MyFtpClientRouteBuilder(protocol, user, password, server, port, filePathAtServer,
filenameAtServer, localPath));
main.start();
Thread.sleep(5000);
main.stop();
String filePath = localPath + File.separator + filenameAtServer;
File f = new File(filePath);
fileExist = f.exists();
if (fileExist) {
f.delete(); // Just delete it.
return Existence.EXIST;
} else {
return Existence.NOT_EXIST;
}
// I CANNOT CATCH THIS EXCEPTION
} catch (Exception e) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Cannot Connect to the Sftp Server " + server);
LOG.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Cannot Connect to the Sftp Server " + server);
LOG.info(e.getMessage());
return Existence.UNKNOWN;
}
}
public static void main(String[] args) throws Exception {
String protocol = "sftp";
int port = 22;
String server;
String user;
String password;
String filePathAtServer;
String filenameAtServer;
boolean fileExist;
Main main = new Main();
server = "unknown.com";
user = "demo";
password = "password";
filePathAtServer = "/";
filenameAtServer = "readme.txt";
doesFileExistAtServer(main, protocol, user, password, server, port, filePathAtServer, filenameAtServer, localPath);
LOG.info("\nThe accesibility of the file 1 " + filenameAtServer + " at the server " + server + " is " + fileExist + "\n")
}
public class MyFtpClientRouteBuilder extends RouteBuilder {
private static final Logger LOG =
LoggerFactory.getLogger(MyFtpClientRouteBuilder.class);
String protocol = "sftp";
int port = 22;
String server;
String user;
String password;
String filePathAtServer;
String filenameAtServer;
String localPath;
boolean fileExist;
public MyFtpClientRouteBuilder(String protocol, String user, String password, String server, int port,
String filePathAtServer, String filenameAtServer, String localPath) {
super();
this.protocol = protocol;
this.user = user;
this.password = password;
this.server = server;
this.port = port;
this.filePathAtServer = filePathAtServer;
this.filenameAtServer = filenameAtServer;
this.localPath = localPath;
}
private static String generateFromUri(String protocol, String user, String password, String server, int port,
String path, String filename) {
final String downloadFalse = "download=false"; // NO, DO NOT Download the file
final String fastExistsCheck = "fastExistsCheck=true";
final String doNothing = "noop=true"; // Just connect, but DO NOTHING
final String connectFail = "throwExceptionOnConnectFailed=true"; // Just in case a connection fails
final String maxReconnect = "maximumReconnectAttempts=0";
final String bridgeError = "consumer.bridgeErrorHandler=true";
return protocol + "://" + user + "@" + server + ":" + port + path + "?" + "fileName=" + filename + "&"
+ downloadFalse + "&"
+ "password=" + password
+ "&" + fastExistsCheck
+ "&" + connectFail
+ "&" + doNothing
+ "&" + maxReconnect
+ "&" + bridgeError;
}
private static String generateLocalUri(String path) {
final String protocol = "file";
final String allowNullBody = "allowNullBody=true";
final String doNothing = "noop=true";
final String fileExist = "fileExist=move";
final String moveExisting = "moveExisting=oldFile";
return protocol + ":" + path + "?" + allowNullBody + "&" + doNothing + "&" + fileExist + "&" + moveExisting;
}
@Override
public void configure() throws CannotConnectException {
final String fromSftpServer = generateFromUri(protocol, user, password, server, port, filePathAtServer, filenameAtServer);
LOG.info("From: " + fromSftpServer);
final String toLocal = generateLocalUri(localPath);
LOG.info("To: " + toLocal);
onException(GenericFileOperationFailedException.class, JSchException.class)
//.handled(true)
.throwException(new CannotConnectException("Cannot connect to the remote SFTP server " + server))
.log("Cannot connect to the remote SFTP server " + server)
.maximumRedeliveries(0)
.to(toLocal)
.continued(false) // Either handled(true) or continue(false) but NOT both together
.end();
from(fromSftpServer).to(toLocal);
}
}