java Ftp文件从java代码下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10756438/
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
Ftp File download From java code
提问by nomee.tec
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 i m 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.openDataConnection(FTPClient.java:667) at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1595) at FtpDownloadDemo.main(FtpDownloadDemo.java:25)
java.net.SocketException: 软件导致连接中止:在 java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) 在 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) 在 org.apache.commons.net.ftp.FTP.__send(FTP.java:496) 在 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)在 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)
回答by nomee.tec
turn off the firewall.in this issue
关闭防火墙。在这个问题
回答by milletron
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. Try just "a.txt" in whichever directory your ftp client is set to dump a user into.
2) 可能是因为您正在尝试获取远程文件“/a.txt”,因此您正在尝试访问根目录,而您的 ftp 服务器未设置为允许您访问。尝试在您的 ftp 客户端设置为将用户转储到的任何目录中的“a.txt”。
回答by The_Cute_Hedgehog
The documentation on the given example code that you should call the enterLocalPassiveModemethod of FTPClient if you are behind a firewall.
如果您在防火墙后面,您应该调用FTPClient的enterLocalPassiveMode方法的给定示例代码的文档。
回答by gaspa85
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
WORK AROUND 解决这个问题启动程序
-Djava.net.preferIPv4Stack=true
-Djava.net.preferIPv4Stack=true