java.net.SocketException:权限被拒绝:recv 失败
java.net.SocketException: Permission denied: recv failed
我是这个网络 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)
我更新了我的 windows,并下载了所有更新
我允许 java.exe 和 javaw.exe 在我的防火墙中的入站和出站连接类型
请帮帮我,我完全卡在这里了。
注意:由于我还在学习,我还没有编写您注销和断开连接的那部分代码。我希望它与此错误无关。
原来不仅要下载安装更新,还要重启电脑!
好吧,所以我不得不重新启动它!
问题已解决。
谢谢!
我是这个网络 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)
我更新了我的 windows,并下载了所有更新
我允许 java.exe 和 javaw.exe 在我的防火墙中的入站和出站连接类型
请帮帮我,我完全卡在这里了。
注意:由于我还在学习,我还没有编写您注销和断开连接的那部分代码。我希望它与此错误无关。
原来不仅要下载安装更新,还要重启电脑!
好吧,所以我不得不重新启动它!
问题已解决。
谢谢!