我是这个网络java概念的初学者程序员,你可以连接到远程ftp服务器并执行琐碎的任务。
这是我的代码
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class SimpleOne {
static String serverName = "ftp.drivehq.com";
static int port = 21;
static String username = "CANTSHOW";
static String password = "CANTSHOW";
public static void main(String[] args) throws IOException{
FTPClient ftpc = new FTPClient();
ftpc.connect(serverName, port);
int reply = ftpc.getReplyCode();
String sReply = ftpc.getReplyString();
if(!FTPReply.isPositiveCompletion(reply))
{
System.out.println("Some error!");
return;
}
boolean success = ftpc.login(username, password);
if(success)
{
System.out.println("Login successful!");
System.out.println(sReply);
ftpc.enterLocalPassiveMode();
String dir=ftpc.printWorkingDirectory();
System.out.println(dir);
success = ftpc.changeWorkingDirectory("My Documents");
if(success)
{
System.out.println(ftpc.printWorkingDirectory());
ftpc.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "SampleText.txt";
File localFile = new File("sample.txt");
OutputStream localOutputStream = new BufferedOutputStream(new FileOutputStream(localFile));
success=ftpc.retrieveFile(remoteFile, localOutputStream);//<--- ERROR LINE
localOutputStream.close();
if(!success)
{
System.out.println("There was some problem in retrieving the file");
return;
}
System.out.println("File was downloaded!");
}
else
{
System.out.println("Could not change directory!");
}
}
else
{
System.out.println("Login failed!");
}
}
}
以下是我的输出:
Login successful!
220 Welcome to the most popular FTP hosting service! Save on hardware, software, hosting and admin. Share files/folders with read-write permission. Visit http://www.drivehq.com/ftp/;
/
/My Documents
Exception in thread "main" java.net.SocketException: Permission denied: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:582)
at org.apache.commons.net.ftp.FTP.pasv(FTP.java:1007)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:869)
at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:1854)
at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1845)
at SimpleOne.main(SimpleOne.java:44)
我所做的一切:
我搜索了这个问题,发现它是作为官方错误提交的,微软发布了一个修补程序。我下载了该修补程序,但安装程序说它不适用于我的Windows版本(顺便说一句,我使用win7 x86和jdk 1.7.0_67)
我更新了我的窗口,并下载了所有更新
我在入站和出站连接类型的防火墙中允许使用java.exe和javaw.exe
请帮助我,我完全被困在这里。
注意:由于我还在学习,我还没有写出你注销和断开的部分代码。我希望它与此错误无关。
答案 0 :(得分:1)
事实证明,您不仅需要下载并安装更新,还必须重新启动电脑!
好吧,我只需要重新启动它!
问题解决了。
三江源!