简体   繁体   English

使用Java在CentOS ec2实例上进行FTP

[英]FTP on CentOS ec2 instance using Java

Having a problem with FTP transfer from our server (FileZilla, setup in passive mode) to our web application deployed on a CentOS ec2 instance. FTP从我们的服务器(FileZilla,以被动模式设置)到部署在CentOS ec2实例上的Web应用程序的FTP传输有问题。 Manually testing with wget and ftp from the instance, we are able to authenticate, change directory, and fetch files. 从实例wget和FTP手动测试,我们能够进行身份验证,改变目录,并获取文件。 However, our ec2 deployed webapp does not fetch the files, although it DOES authenticate and navigate successfully. 但是,部署了ec2的webapp确实可以进行身份​​验证和导航,但无法获取文件。 FTP upload in our app works fine on our LAN, and also testing externally (dev laptop on wireless tether, no VPN) 应用程序中的FTP上传可以在我们的局域网上正常工作,也可以在外部进行测试(无线网络上的开发笔记本电脑,没有VPN)

We have written more focused unit tests as part of this and are currently reproducing the failure and success cases with the following, deployed from ec2 (failure) and non-ec2 (success): 作为此过程的一部分,我们编写了更加集中的单元测试,并且目前正在重现使用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 server logfile is identical through login, USER, PASS, CWD, but differs once transfer begins. FileZilla服务器日志文件通过登录名,USER,PASS,CWD相同,但是一旦传输开始就不同。

Success case is as follows: 成功案例如下:

(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.

Failure case: 失败案例:

(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.

try calling out.flush() or out.close() at the end of your test method. 尝试在测试方法的末尾调用out.flush()或out.close()。 Seems main thread is dying without flushing the content to server. 似乎主线程快死了,没有将内容刷新到服务器。

The success case uses passive mode while the failure case uses extended passive mode. 成功案例使用被动模式,而失败案例使用扩展被动模式。 You may have a hard time fine tuning the FTP connection using a plain URLConnection. 使用普通的URLConnection可能很难调整FTP连接。 I strongly suggest using a java client library instead, such as Apache Commons Net . 我强烈建议改用Java客户端库,例如Apache Commons Net You will find an FTP example on the home page. 您会在主页上找到一个FTP示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM