简体   繁体   English

ftp文件下载from java code

[英]Ftp File download From java code

i have this code to download a single file . 我有此代码下载单个文件。

    import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
public class NetTest {
public static void main(String[] args){
FTPClient client = new FTPClient( );
OutputStream outStream;
    try {

this is the part of server and passwords . 这是服务器和密码的一部分。

   client.connect( "servername" );
    client.login("noman123", "pass");
    String remoteFile = "/a.txt";
    outStream = new FileOutputStream( "a.txt" );

simple fill downloading but error on this line 简单填充下载,但此行错误

    client.retrieveFile( remoteFile, outStream );
} catch(IOException ioe) {
    System.out.println( "Error communicating with FTP server." );
} finally {
    try {
        client.disconnect( );
    } catch (IOException e) {
        System.out.println( "Problem disconnecting from FTP server" );
    }
}

}
}

and it gives me errors like i hope that u can understand the issue that im facing now 它给了我我希望您能理解我现在面临的问题的错误

java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295) at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141) at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229) at java.io.BufferedWriter.flush(BufferedWriter.java:254) at org.apache.commons.net.ftp.FTP.__send(FTP.java:496) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:470) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547) at org.apache.commons.net.ftp.FTP.port(FTP.java:872) at org.apache.commons.net.ftp.FTPClient. java.net.SocketException:软件导致连接中止:java.net.SocketOutputStream.socketWrite0(本地方法)处的java.net.SocketOutputStream.socketWrite0(本地方法)处的套接字写入错误,java.net.SocketOutputStream.write( SocketOutputStream.java:153)位于sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)位于sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)在sun.nio.cs.StreamEncoder.implFlush (sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)处的StreamEncoder.java:295)java.io.BufferedWriter.flush(BufferedWriter)处的java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)处的(StreamEncoder.java:141) java:254)位于org.apache.commons.net.ftp.FTP.FTP .__ send(FTP.java:496)位于org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:470)位于org.apache org.apache.commons.net.ftp.FTP.port(FTP.java:872)上的.commons.net.ftp.FTP.sendCommand(FTP.java:547)在org.apache.commons.net.ftp.FTPClient上。 openDataConnection (FTPClient.java:667) at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1595) at FtpDownloadDemo.main(FtpDownloadDemo.java:25) openDataConnection(FTPClient.java:667)在org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1595)在FtpDownloadDemo.main(FtpDownloadDemo.java:25)

关闭防火墙。在此问题中

Make sure you can ping the server and log in manually, but assuming you're ok there, there's two additional things I would do. 确保可以ping服务器并手动登录,但是假设您还可以,那么我还要做两件事。

1) Per the FTP Client documentation, check that you're really connected 1)根据FTP客户端文档,检查您是否确实已连接

// After connection attempt, you should check the reply code to verify success.
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
        { // print more complete error }

There's a full example here . 有一个完整的例子在这里

2) It could be since you're trying to get the remote file "/a.txt" you're trying to get to the root directory and your ftp server isn't set up to allow you access. 2)可能是由于您试图获取远程文件“ /a.txt”而您试图获取根目录,而您的ftp服务器未设置为允许您访问。 Try just "a.txt" in whichever directory your ftp client is set to dump a user into. 尝试将ftp客户端设置为将用户转储到的目录中的“ a.txt”。

如果位于防火墙后面,则应调用给定示例代码的文档,您应该调用FTPClient的enterLocalPassiveMode方法。

read this article http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7077696 阅读本文http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7077696

WORK AROUND to solve this problem start program with 解决此问题的方法启动程序

-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack = true

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

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