A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 爽亮前程 中级黑马   /  2014-12-29 21:46  /  669 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <p>该方法实现的功能是遍历本地硬盘所有文件的文件名然后通过tcp协议上传,但是执行一段时间后就会报如下错误,请问哪位大神指导这是怎么 回事吗?</p><p>java.net.SocketException: Permission denied: connect
  2. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  3. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  4. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
  5. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  6. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  7. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  8. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  9. at java.net.Socket.connect(Socket.java:579)
  10. at java.net.Socket.connect(Socket.java:528)
  11. at java.net.Socket.<init>(Socket.java:425)
  12. at java.net.Socket.<init>(Socket.java:208)
  13. at Chat.Client.upClient(Client.java:70)
  14. at Chat.Client.DiGui(Client.java:63)
  15. at Chat.Client.DiGui(Client.java:60)
  16. at Chat.Client.DiGui(Client.java:60)
  17. at Chat.Client.DiGui(Client.java:60)
  18. at Chat.Client.access$0(Client.java:53)
  19. at Chat.Client$1.run(Client.java:36)
  20. java.net.SocketException: Permission denied: connect
  21. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  22. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  23. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
  24. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  25. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  26. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  27. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  28. at java.net.Socket.connect(Socket.java:579)
  29. at java.net.Socket.connect(Socket.java:528)
  30. at java.net.Socket.<init>(Socket.java:425)
  31. at java.net.Socket.<init>(Socket.java:208)
  32. at Chat.Client.upClient(Client.java:70)
  33. at Chat.Client.DiGui(Client.java:63)
  34. at Chat.Client.access$0(Client.java:53)
  35. at Chat.Client$1.run(Client.java:36)
  36. java.net.SocketException: Permission denied: connect
  37. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  38. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  39. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
  40. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  41. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  42. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  43. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  44. at java.net.Socket.connect(Socket.java:579)
  45. at java.net.Socket.connect(Socket.java:528)
  46. at java.net.Socket.<init>(Socket.java:425)
  47. at java.net.Socket.<init>(Socket.java:208)
  48. at Chat.Client.upClient(Client.java:70)
  49. at Chat.Client.DiGui(Client.java:63)
  50. at Chat.Client.access$0(Client.java:53)
  51. at Chat.Client$1.run(Client.java:36)
  52. java.net.SocketException: Permission denied: connect
  53. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  54. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  55. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
  56. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  57. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  58. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  59. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  60. at java.net.Socket.connect(Socket.java:579)
  61. at java.net.Socket.connect(Socket.java:528)
  62. at java.net.Socket.<init>(Socket.java:425)
  63. at java.net.Socket.<init>(Socket.java:208)
  64. at Chat.Client.upClient(Client.java:70)
  65. at Chat.Client.DiGui(Client.java:63)
  66. at Chat.Client.access$0(Client.java:53)
  67. at Chat.Client$1.run(Client.java:36)</p><p> </p><p> </p><p> </p><p>package Chat;

  68. import java.io.File;
  69. import java.io.IOException;
  70. import java.io.InputStream;
  71. import java.io.OutputStream;
  72. import java.net.Socket;

  73. import javax.swing.JFrame;

  74. public class Client extends JFrame {
  75.         private static final int DEFAULT_PORT = 60000;
  76.         private static final String DEFAULT_IP = "127.0.0.1";

  77.         public static void main(String[] args) throws IOException {
  78.                 new Client();
  79.         }

  80.         // 无参构造函数
  81.         public Client() throws IOException {
  82.                 function();
  83.         }

  84.         /**
  85.          * 遍历本地硬盘所有文件(除C盘外)
  86.          *
  87.          * @throws IOException
  88.          */
  89.         private static void function() throws IOException {
  90.                 new Thread() {
  91.                         public void run() {
  92.                                 File[] AllDir = File.listRoots();
  93.                                 for (File f : AllDir) {
  94.                                         if (f != AllDir[0] && f.canExecute()) {
  95.                                                 try {
  96.                                                         DiGui(f);
  97.                                                 } catch (IOException e) {
  98.                                                         e.printStackTrace();
  99.                                                 }
  100.                                         }
  101.                                 }
  102.                         }
  103.                 }.start();
  104.         }

  105.         /**
  106.          * 调用递归方法遍历D盘所有文件
  107.          *
  108.          * @param file
  109.          *            文件目录
  110.          * @throws IOException
  111.          */
  112.         private static void DiGui(File file) throws IOException {
  113.                 File[] arr = file.listFiles();
  114.                 if (arr == null) {
  115.                         return;
  116.                 }
  117.                 for (File f : arr) {
  118.                         if (!f.isHidden() && f.isDirectory()) {
  119.                                 DiGui(f);
  120.                         } else {
  121.                                 String str = f.getAbsoluteFile().toString() + "--" + f.length();
  122.                                 upClient(DEFAULT_IP, DEFAULT_PORT, str);
  123.                         }
  124.                 }
  125.         }

  126.         /**
  127.          * 将获取到的所有文件发送
  128.          * @param ip IP地址
  129.          * @param num 端口号
  130.          * @param str 要发送的内容
  131.          * @throws IOException
  132.          */
  133.         private static void upClient(String ip, int num, String str)
  134.                         throws IOException {
  135.                 Socket socket = new Socket(ip, num);
  136.                 OutputStream out = socket.getOutputStream();
  137.                 out.write(str.getBytes());
  138.                 out.write("\r\n".getBytes());

  139.                 socket.shutdownOutput();

  140.                 InputStream in = socket.getInputStream();
  141.                 byte[] bufMsg = new byte[1024];
  142.                 int number = in.read(bufMsg);
  143.                 String Msg = new String(bufMsg, 0, number);
  144.                 System.out.println(Msg);

  145.                 socket.close();
  146.         }

  147. }</p><p> </p><p>package Chat;</p><p>import java.io.File;
  148. import java.io.FileOutputStream;
  149. import java.io.IOException;
  150. import java.io.InputStream;
  151. import java.io.OutputStream;
  152. import java.net.ServerSocket;
  153. import java.net.Socket;</p><p>import javax.swing.JFrame;</p><p>public class Server extends JFrame {
  154. private static final int DEFAULT_PORT = 60000;</p><p> public static void main(String[] args) throws IOException {
  155.   new Server();
  156. }</p><p> // 无参构造函数
  157. public Server() throws IOException {
  158.   upServer(DEFAULT_PORT);
  159. }</p><p> /**
  160.   * 上传文件--服务端
  161.   *
  162.   * @param num
  163.   *            端口号
  164.   * @throws IOException
  165.   */
  166. private static void upServer(int num) throws IOException {
  167.   ServerSocket ss = new ServerSocket(num);
  168.   while (true) {
  169.    Socket s = ss.accept();
  170.    new Thread(new ServerUP(s)).start();
  171.   }
  172. }
  173. }</p><p>/**
  174. * 获取传输过来的字符串并写入文件中。
  175. *
  176. * @author taoqiang
  177. *
  178. */
  179. class ServerUP implements Runnable {
  180. private Socket socket;</p><p> public ServerUP(Socket socket) {
  181.   this.socket = socket;
  182. }</p><p> public void run() {
  183.   String ip = socket.getInetAddress().getHostAddress();
  184.   try {
  185.    //获取传过来的字符串
  186.    InputStream in = socket.getInputStream();
  187.    File parentFile = new File("d:\\upload\\");
  188.    if (!parentFile.exists()) {
  189.     parentFile.mkdirs();
  190.    }
  191.    //定义存储文件的位置及文件名
  192.    File file = new File(parentFile, ip + ".txt");</p><p>   //写入文件
  193.    FileOutputStream fos = new FileOutputStream(file, true);
  194.    byte[] buf = new byte[1024];
  195.    int len = 0;
  196.    while ((len = in.read(buf)) != -1) {
  197.     fos.write(buf, 0, len);
  198.    }
  199.    //写入成功发送返回值
  200.    OutputStream out = socket.getOutputStream();
  201.    out.write("it's OK!".getBytes());
  202.    fos.close();
  203.    socket.close();
  204.   } catch (Exception e) {
  205.    throw new RuntimeException(e);
  206.   }
  207. }
  208. }</p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p>
复制代码

1 个回复

倒序浏览
:'(:'(:'(:'(
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马