0

从我们的服务器(FileZilla,以被动模式设置)到部署在 CentOS ec2 实例上的 Web 应用程序的 FTP 传输出现问题。从实例中使用 wget 和 ftp 手动测试,我们能够进行身份验证、更改目录和获取文件。然而,我们的 ec2 部署的 webapp 并没有获取文件,尽管它确实成功地进行了身份验证和导航。我们应用程序中的 FTP 上传在我们的 LAN 上运行良好,并且还可以在外部进行测试(无线系绳上的开发笔记本电脑,没有 VPN)

作为其中的一部分,我们编写了更集中的单元测试,目前正在重现失败和成功的案例,这些案例是从 ec2(失败)和非 ec2(成功)部署的:

import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class FTPTest {

    @Test
    public void testFTPAccess() throws IOException {
        String fileUrl = "ftp://username:password@ftp.site.com/path/to/resource.txt";
        URL url = new URL(fileUrl);
        URLConnection uc = url.openConnection();
        InputStream is = uc.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024*10);
        com.google.common.io.ByteStreams.copy(is,out);
        System.out.println(new String(out.toByteArray()));
    }
}

FileZilla 服务器日志文件通过登录、USER、PASS、CWD 是相同的,但在传输开始后会有所不同。

成功案例如下:

(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> Connected, sending welcome message...
(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> 220-FileZilla Server version 0.9.24 beta
(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> 220 Connected to server FTP!
(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> USER ftpUser
(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> 331 Password required for ftpUser
(000216) 8/28/2012 10:47:20 AM - (not logged in) (external IP)> PASS *********
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> 230 Logged on
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> TYPE I
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> 200 Type set to I
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> CWD DPS
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> 250 CWD successful. "/DPS" is current directory.
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> CWD DP276
(000216) 8/28/2012 10:47:20 AM - ftpUser (external IP)> 250 CWD successful. "/DPS/DP276" is current directory.
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> PASV ALL
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> 227 Entering Passive Mode (FTP Server IP4,190)
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> PASV
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> 227 Entering Passive Mode (FTP Server IP4,191)
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> RETR sales.txt
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> 150 Connection accepted
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> 226 Transfer OK
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> QUIT
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> 221 Goodbye
(000216) 8/28/2012 10:47:21 AM - ftpUser (external IP)> disconnected.

失败案例:

(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> Connected, sending welcome message...
(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> 220-FileZilla Server version 0.9.24 beta
(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> 220 Connected to server FTP!
(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> USER ftpUser
(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> 331 Password required for ftpUser
(000217) 8/28/2012 11:25:11 AM - (not logged in) (ec2 Instance IP)> PASS *********
(000217) 8/28/2012 11:25:11 AM - ftpUser (ec2 Instance IP)> 230 Logged on
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> TYPE I
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 200 Type set to I
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> CWD DPS
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 250 CWD successful. "/DPS" is current directory.
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> CWD DP276
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 250 CWD successful. "/DPS/DP276" is current directory.
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> EPSV ALL
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 229 Entering Extended Passive Mode (|||1318|)
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> EPSV
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 229 Entering Extended Passive Mode (|||1319|)
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> EPRT |1|ec2 internal IP|37245|
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 200 Port command successful
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> RETR products.txt
(000217) 8/28/2012 11:25:12 AM - ftpUser (ec2 Instance IP)> 150 Opening data channel for file transfer.
(000217) 8/28/2012 11:25:23 AM - ftpUser (ec2 Instance IP)> 425 Can't open data connection.
4

2 回答 2

0

尝试在测试方法结束时调用 out.flush() 或 out.close()。似乎主线程正在死亡而没有将内容刷新到服务器。

于 2012-08-30T05:35:01.347 回答
0

成功案例使用被动模式,而失败案例使用扩展被动模式。您可能很难使用纯 URLConnection 微调 FTP 连接。我强烈建议改用 java 客户端库,例如Apache Commons Net。您将在主页上找到一个 FTP 示例。

于 2012-08-30T15:28:06.613 回答