黑马程序员技术交流社区
标题:
有人知道这个问题怎么解决吗?
[打印本页]
作者:
爽亮前程
时间:
2014-12-29 21:46
标题:
有人知道这个问题怎么解决吗?
<p>该方法实现的功能是遍历本地硬盘所有文件的文件名然后通过tcp协议上传,但是执行一段时间后就会报如下错误,请问哪位大神指导这是怎么 回事吗?</p><p>java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Chat.Client.upClient(Client.java:70)
at Chat.Client.DiGui(Client.java:63)
at Chat.Client.DiGui(Client.java:60)
at Chat.Client.DiGui(Client.java:60)
at Chat.Client.DiGui(Client.java:60)
at Chat.Client.access$0(Client.java:53)
at Chat.Client$1.run(Client.java:36)
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Chat.Client.upClient(Client.java:70)
at Chat.Client.DiGui(Client.java:63)
at Chat.Client.access$0(Client.java:53)
at Chat.Client$1.run(Client.java:36)
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Chat.Client.upClient(Client.java:70)
at Chat.Client.DiGui(Client.java:63)
at Chat.Client.access$0(Client.java:53)
at Chat.Client$1.run(Client.java:36)
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Chat.Client.upClient(Client.java:70)
at Chat.Client.DiGui(Client.java:63)
at Chat.Client.access$0(Client.java:53)
at Chat.Client$1.run(Client.java:36)</p><p> </p><p> </p><p> </p><p>package Chat;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JFrame;
public class Client extends JFrame {
private static final int DEFAULT_PORT = 60000;
private static final String DEFAULT_IP = "127.0.0.1";
public static void main(String[] args) throws IOException {
new Client();
}
// 无参构造函数
public Client() throws IOException {
function();
}
/**
* 遍历本地硬盘所有文件(除C盘外)
*
* @throws IOException
*/
private static void function() throws IOException {
new Thread() {
public void run() {
File[] AllDir = File.listRoots();
for (File f : AllDir) {
if (f != AllDir[0] && f.canExecute()) {
try {
DiGui(f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();
}
/**
* 调用递归方法遍历D盘所有文件
*
* @param file
* 文件目录
* @throws IOException
*/
private static void DiGui(File file) throws IOException {
File[] arr = file.listFiles();
if (arr == null) {
return;
}
for (File f : arr) {
if (!f.isHidden() && f.isDirectory()) {
DiGui(f);
} else {
String str = f.getAbsoluteFile().toString() + "--" + f.length();
upClient(DEFAULT_IP, DEFAULT_PORT, str);
}
}
}
/**
* 将获取到的所有文件发送
* @param ip IP地址
* @param num 端口号
* @param str 要发送的内容
* @throws IOException
*/
private static void upClient(String ip, int num, String str)
throws IOException {
Socket socket = new Socket(ip, num);
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.write("\r\n".getBytes());
socket.shutdownOutput();
InputStream in = socket.getInputStream();
byte[] bufMsg = new byte[1024];
int number = in.read(bufMsg);
String Msg = new String(bufMsg, 0, number);
System.out.println(Msg);
socket.close();
}
}</p><p> </p><p>package Chat;</p><p>import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;</p><p>import javax.swing.JFrame;</p><p>public class Server extends JFrame {
private static final int DEFAULT_PORT = 60000;</p><p> public static void main(String[] args) throws IOException {
new Server();
}</p><p> // 无参构造函数
public Server() throws IOException {
upServer(DEFAULT_PORT);
}</p><p> /**
* 上传文件--服务端
*
* @param num
* 端口号
* @throws IOException
*/
private static void upServer(int num) throws IOException {
ServerSocket ss = new ServerSocket(num);
while (true) {
Socket s = ss.accept();
new Thread(new ServerUP(s)).start();
}
}
}</p><p>/**
* 获取传输过来的字符串并写入文件中。
*
* @author taoqiang
*
*/
class ServerUP implements Runnable {
private Socket socket;</p><p> public ServerUP(Socket socket) {
this.socket = socket;
}</p><p> public void run() {
String ip = socket.getInetAddress().getHostAddress();
try {
//获取传过来的字符串
InputStream in = socket.getInputStream();
File parentFile = new File("d:\\upload\\");
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//定义存储文件的位置及文件名
File file = new File(parentFile, ip + ".txt");</p><p> //写入文件
FileOutputStream fos = new FileOutputStream(file, true);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
//写入成功发送返回值
OutputStream out = socket.getOutputStream();
out.write("it's OK!".getBytes());
fos.close();
socket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}</p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p>
复制代码
作者:
爽亮前程
时间:
2014-12-31 19:01
:'(:'(:'(:'(
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2