java 通过java连接到ftps站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5825050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
connect to ftps site via java
提问by Vivek
I am trying to connect to a clients ftps server via my java code. I am using apache commons library to do so. However, I am not able to do so. Can anyone please help me with this.
我正在尝试通过我的 java 代码连接到客户端 ftps 服务器。我正在使用 apache 公共库来这样做。但是,我无法这样做。任何人都可以帮我解决这个问题。
The client server uses FTPS/Implicit SSL connection and uses Passive mode for data Connections.
客户端服务器使用 FTPS/隐式 SSL 连接,并使用被动模式进行数据连接。
My code is as follows:
我的代码如下:
public static void connectServer(String host, int port, String userName, String password) throws Exception {
FTPSClient client = new FTPSClient("SSL", true);
String remote = "Contact.xml";
File inFile = new File("C:/Documents/Applications/Contact.xml");
File outFile = new File("C:/Documents/Applications/Sample.xml");
InputStream input = new FileInputStream(inFile);
InputStream out = new FileInputStream(outFile);
try {
if(client.isConnected()){
client.disconnect();
}
client.connect(host,990);
client.enterLocalPassiveMode();
client.enterRemotePassiveMode();
client.login(userName, password);
client.setBufferSize((int)inFile.length()+100);
client.completePendingCommand();
System.out.println(client.printWorkingDirectory());
System.out.println(inFile.getAbsolutePath());
client.storeFile(remote, input);
out = client.retrieveFileStream("/folder/inputfeed.xml");
client.completePendingCommand();
client.logout();
} catch (Exception e) {
e.printStackTrace();
System.err.println(client.getReplyString());
} finally {
out.close();
input.close();
client.disconnect();
}
}
This code does not throw any exception, but I don't see the file being copied to server, neither any data being copied to my InputStream. Also, sysout statement for getting the working directory returns me the correct directory. I am also able to connect to server via Filezilla and WinSCP.
此代码不会引发任何异常,但我没有看到文件被复制到服务器,也没有任何数据被复制到我的 InputStream。此外,用于获取工作目录的 sysout 语句会返回正确的目录。我还可以通过 Filezilla 和 WinSCP 连接到服务器。
Please help, I am getting stuck with this.
请帮忙,我被这个卡住了。
Thanks
谢谢
回答by Vladimir Dyuzhev
Why are you entering passive mode before login()?
为什么在 login() 之前进入被动模式?
I suspect that could be the issue, because the symptoms are those of active mode, where the connection cannot be established due to FW rule DROP (not REJECT; when rejected the exception is thrown right away, but DROP can hang forever).
我怀疑这可能是问题所在,因为症状是活动模式的症状,由于 FW 规则 DROP(不是 REJECT;拒绝时立即抛出异常,但 DROP 可以永远挂起),无法建立连接。
P.S. Also, not clear what is "remote" passive mode; the only one that makes difference is "local".
PS 另外,不清楚什么是“远程”被动模式;唯一不同的是“本地”。
回答by jbak
enterRemotePassiveMode();
is used only for server to server connections, not client to server. Remove that line.
enterRemotePassiveMode();
仅用于服务器到服务器的连接,而不是客户端到服务器的连接。删除该行。
回答by sheu
client.connect(host,990);
Why are using the hardcoded port number? I suggest you change this line to use the port number passed to you function/method
client.connect(host,990);
为什么使用硬编码的端口号?我建议您更改此行以使用传递给您的函数/方法的端口号
For reasons that I did not have time to look at you code hangs and this line
由于我没有时间看你代码挂起和这一行的原因
client.completePendingCommand();
before the server disconnects it. I am not sure if you are not getting the exception or you are just not waiting long enough for you code to throw the exception
在服务器断开连接之前。我不确定您是否没有收到异常,或者您只是没有等待足够长的时间让您的代码抛出异常
回答by Robert E
You need to check status after each client(socket)
method.
Especially after client.connect()
and client.login()
.
您需要在每种client(socket)
方法后检查状态。特别是在client.connect()
和之后client.login()
。
Example:
例子:
client.connect(host, 990);
System.out.print(client.getReplyString());
...
client.login(username,password);
System.out.print(client.getReplyString());
If the method failed (error), the getReplyString()
is usually very descriptive about what you did wrong.
如果方法失败(错误),getReplyString()
通常会非常详细地说明您做错了什么。
回答by Ashish Shetkar
The Port Number you use for FTP - 21 and for SFTP is 22
您用于 FTP - 21 和 SFTP 的端口号是 22
I has some what same requirement , to download file from different servers , so some times i had to use 21 and some time 22 .
我有一些相同的要求,从不同的服务器下载文件,所以有时我不得不使用 21 和一些时间 22 。
I tried below approach
我尝试了以下方法
FTPClient ftpClient = new FTPClient();
int port = 21;
try {
try{
ftpClient.connect(serverName, port);
}catch(Exception e){
ftpClient.connect(serverName, 22);
}
try {
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(workingDir);
String fileName = "fileToDownLoad";
File downloadFile = new File(fileName);
OutputStream localOutputStream = new BufferedOutputStream(
new FileOutputStream(downloadFile));
InputStream fptInputStream = ftpClient.retrieveFileStream(folder
+ "/" + file);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = fptInputStream.read(bytesArray)) != -1) {
localOutputStream.write(bytesArray, 0, bytesRead);
}
boolean success = ftpClient.completePendingCommand();
if (success) {
}
localOutputStream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}