{"id":194030,"date":"2020-10-31T13:02:36","date_gmt":"2020-10-31T05:02:36","guid":{"rendered":"http:\/\/4563.org\/?p=194030"},"modified":"2020-10-31T13:02:36","modified_gmt":"2020-10-31T05:02:36","slug":"java-socket-%e6%96%87%e4%bb%b6%e4%bc%a0%e8%be%93%e9%97%ae%e9%a2%98%ef%bc%8c%e8%be%93%e5%85%a5%e6%b5%81%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e9%98%bb%e5%a1%9e%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=194030","title":{"rendered":"Java Socket \u6587\u4ef6\u4f20\u8f93\u95ee\u9898\uff0c\u8f93\u5165\u6d41\u8bfb\u53d6\u6587\u4ef6\u963b\u585e\u600e\u4e48\u89e3\u51b3\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java Socket \u6587\u4ef6\u4f20\u8f93\u95ee\u9898\uff0c\u8f93\u5165\u6d41\u8bfb\u53d6\u6587\u4ef6\u963b\u585e\u600e\u4e48\u89e3\u51b3\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : industryhive <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u81ea\u5df1\u7528 java socket \u5199\u4e86\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d\u7684\u5c0f\u5de5\u5177\uff0c\u5206\u4e3a\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u4e0a\u4f20\u6587\u4ef6\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u6587\u4ef6\u3002\u4f46\u662f\u90e8\u7f72\u4e4b\u540e\uff0c\u670d\u52a1\u7aef\u7684\u8f93\u5165\u6d41\u603b\u662f\u88ab\u963b\u585e\uff0c\u6709\u5927\u4f6c\u80fd\u770b\u51fa\u662f\u4ec0\u4e48\u95ee\u9898\u5417\uff1f \u5ba2\u6237\u7aef\u4ee3\u7801\uff1a public static String upload(String fileName, FileInputStream fis) { if (fis == null) { System.out.println(&#8220;FileInputStream Is Null&#8221;); return null; } Socket storageSocket = null; DataOutputStream dos = null; DataInputStream dis = null; String storageAddr = null; try { System.out.println(&#8220;00000000000&#8221;); RapidMessage contactMsg = Contactor.contact(BaseProtocol.UPLOAD_COMMAND); if (contactMsg.getStatus() != 200) { System.out.println(contactMsg.getResult()); return null; } System.out.println(contactMsg.getResult()); storageSocket = new Socket(InetAddress.getByName(contactMsg.getResult()), Integer.parseInt(ConfigLoader.STORAGE_PORT)); dos = new DataOutputStream(storageSocket.getOutputStream()); dos.write(Contactor.getHeader(BaseProtocol.UPLOAD_COMMAND)); dos.writeUTF(fileName); byte[] fileBytes = new byte[1024]; System.out.println(&#8220;11111111111&#8221;); int length; while ((length = fis.read(fileBytes)) != -1) { System.out.println(&#8220;&#8212;&#8212;&#8220;); dos.write(fileBytes, 0, length); } System.out.println(&#8220;22222222222&#8221;); storageSocket.shutdownOutput(); dis = new DataInputStream(storageSocket.getInputStream()); System.out.println(&#8220;33333333333&#8221;); int status = dis.read(); System.out.println(&#8220;44444444444&#8221;); String result = dis.readUTF(); System.out.println(&#8220;55555555555&#8221;); if (status != 200) { System.out.println(result); return null; } System.out.println(&#8220;66666666666&#8221;); storageAddr = result; System.out.println(&#8220;Upload Success: &#8221; + storageAddr); } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); if (dis != null) { dis.close(); } if (dos != null) { dos.close(); } if (storageSocket != null) { storageSocket.close(); } } catch (IOException e) { e.printStackTrace(); } } return storageAddr; } \u670d\u52a1\u7aef\u4ee3\u7801\uff1a public static void upload(Socket connection) {<\/p>\n<pre><code>    System.out.println(\"eeeeeeeeeeeeeeeeeeeeeeeeeeeee\");      DataInputStream dis = null;     FileOutputStream fos = null;     try {          System.out.println(\"ffffffffffffffffffffffffffff\");          dis = new DataInputStream(connection.getInputStream());          System.out.println(\"ggggggggggggggggggggggggggggg\");          String stage_path = Initializer.STORE_PATH + \"\/\" + Initializer.currentStage;         File stagePath = new File(stage_path);          System.out.println(\"hhhhhhhhhhhhhhhhhhhhhhhhhhhhh\");          if (!stagePath.exists()) {             Logger.error(\"Path Not Found: \" + stage_path);             Sweeper.close(connection, BaseProtocol.RESPONSE_FAILURE, \"Path Not Found: \" + stage_path);             return;         }         if (!stagePath.isDirectory()) {             Logger.error(\"Path Is Not A Directory: \" + stage_path);             Sweeper.close(connection, BaseProtocol.RESPONSE_FAILURE, \"Path Is Not A Directory: \" + stage_path);             return;         }          System.out.println(\"iiiiiiiiiiiiiiiiiiiiiiiiiiiii\");          String storageId = Integer.toHexString(Integer.parseInt(Initializer.STORAGE_ID));          System.out.println(\"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj\");          if (storageId.length() == 1) storageId = \"0\" + storageId;         String encoding = \"N\" + storageId + new Date().getTime() + (int) (Math.random() * 90 + 10);         byte[] enSrc = encoding.getBytes(StandardCharsets.UTF_8);         String encoded = Base64.getEncoder().encodeToString(enSrc);          System.out.println(\"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\");          String realName = dis.readUTF();          System.out.println(\"lllllllllllllllllllllllllllllllllll\");          String fileName;         if (realName.contains(\".\")) {             String extension = realName.substring(realName.lastIndexOf(\".\"));             fileName = encoded + extension;         } else {             fileName = encoded;         }          System.out.println(\"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm\");          File file = new File(stage_path + \"\/\" + fileName);         fos = new FileOutputStream(file);         byte[] bytes = new byte[1024];         int length;          System.out.println(\"nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn\");          while ((length = dis.read(bytes)) != -1) {             fos.write(bytes, 0, length);         }          System.out.println(\"oooooooooooooooooooooooooooooooo\");          String uploadPath = \"group\" + Initializer.GROUP_ID + \"\/M00\/\" + Initializer.currentStage + \"\/\" + fileName;         Logger.info(\"Upload File Success: \" + uploadPath);          System.out.println(\"ppppppppppppppppppppppppppppppppp\");          Sweeper.close(connection, BaseProtocol.RESPONSE_SUCCESS, uploadPath);         String[] filesNum = stagePath.list();         if (filesNum != null &amp;&amp; filesNum.length &gt; 99) {             Initializer.stageIndex++;             Initializer.currentStage = Initializer.STAGE_NAMES[Initializer.stageIndex];             Initializer.nextStage = Initializer.STAGE_NAMES[Initializer.stageIndex + 1];         }         System.out.println(\"uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\");     } catch (IOException e) {         e.printStackTrace();     } finally {         try {             if (fos != null) {                 fos.close();             }             if (dis != null) {                 dis.close();             }             if (connection != null) {                 connection.close();             }         } catch (IOException e) {             e.printStackTrace();         }     } } <\/code><\/pre>\n<p>\u670d\u52a1\u7aef\u4ee3\u7801\u5728\u8f93\u51fa\u201ckkkkkkkkkkkkkkkkkkkkkkk\u201d\u4e4b\u540e\u5c31\u4e0d\u518d\u8f93\u51fa\u4e86\uff0c\u53c8\u8bd5\u8fc7\u51e0\u6b21\u4e4b\u540e\u670d\u52a1\u7aef\u4ee3\u7801\u6709\u51e0\u6b21\u4e5f\u4f1a\u5728\u8f93\u51fa\u201cnnnnnnnnnnnnnnnnnnnn\u201d\u4e4b\u540e\u4e0d\u518d\u8f93\u51fa\u3002\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f\u96be\u9053 DataInputStream.readUTF()\u4e5f\u4f1a\u963b\u585e\u5417\uff1f\u8fd8\u6709\u96be\u9053\u5373\u4f7f\u5ba2\u6237\u7aef\u5728\u8c03\u7528 socket.shutdownoutput()\u4e4b\u540e\uff0c\u8f93\u5165\u7aef DataInputStream.read(byte[])\u4ecd\u7136\u4f1a\u963b\u585e\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java Socket \u6587\u4ef6\u4f20\u8f93\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194030"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194030"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194030\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194030"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}