{"id":194052,"date":"2020-10-31T23:05:49","date_gmt":"2020-10-31T15:05:49","guid":{"rendered":"http:\/\/4563.org\/?p=194052"},"modified":"2020-10-31T23:05:49","modified_gmt":"2020-10-31T15:05:49","slug":"java-socket-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%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=194052","title":{"rendered":"Java Socket \u6587\u4ef6\u4e0a\u4f20\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\u4e0a\u4f20\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> 4<\/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<\/p>\n<p>\u5ba2\u6237\u7aef\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static String upload(String fileName, FileInputStream fis) {     if (fis == null) {         System.out.println(\"FileInputStream Is Null\");         return null;     }     Socket storageSocket = null;     DataOutputStream dos = null;     DataInputStream dis = null;     String filePath = null;     try {         System.out.println(\"00000000000\");         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(\"11111111111\");         int length;         while ((length = fis.read(fileBytes)) != -1) {             System.out.println(\"------\");             dos.write(fileBytes, 0, length);         }         System.out.println(\"22222222222\");         storageSocket.shutdownOutput();         dis = new DataInputStream(storageSocket.getInputStream());         System.out.println(\"33333333333\");         int status = dis.read();         System.out.println(\"44444444444\");         String result = dis.readUTF();         System.out.println(\"55555555555\");         if (status != 200) {             System.out.println(result);             return null;         }         System.out.println(\"66666666666\");         filePath = result;         System.out.println(\"Upload Success: \" + filePath);     } 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 filePath; } <\/code><\/pre>\n<p>\u670d\u52a1\u7aef\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void upload(Socket connection) {      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\u53d1\u73b0\uff0c\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>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4198355\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : laminux29 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u6b21\u5199\u590d\u6742\u7684\u4e1c\u897f\uff0c\u5efa\u8bae\u5148\u522b\u6025\u7740\u7f16\u7801\uff0c\u5148\u753b\u6d41\u7a0b\u56fe\u3002<\/p>\n<p>\u7136\u540e\u5bf9\u7167\u6d41\u7a0b\u56fe\uff0c\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<p>\u51fa\u4e86\u95ee\u9898\uff0c\u6309\u7167\u6d41\u7a0b\u56fe\uff0c\u5bf9\u6bcf\u4e2a\u73af\u8282\u8fdb\u884c\u8c03\u8bd5\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5230\u4e86\u8fd9\u91cc\uff0c\u95ee\u9898\u80fd\u81ea\u5df1\u89e3\u51b3\u3002<\/p>\n<p>\u5982\u679c\u8fd8\u89e3\u51b3\u4e0d\u4e86\uff0c\u628a\u6d41\u7a0b\u56fe\u548c\u4ee3\u7801\u4e00\u8d77\u53d1\u51fa\u6765\uff0c\u5e76\u5728\u4ee3\u7801\u91cc\uff0c\u6ce8\u660e\u6bcf\u6bb5\u4ee3\u7801\uff0c\u5bf9\u5e94\u7684\u662f\u54ea\u5757\u6d41\u7a0b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4198356\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzh7982 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5ba2\u6237\u7aef socket.close()\u4e4b\u540e\u770b\u770b \u670d\u52a1\u7aef\u8fd8\u963b\u585e\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java Socket \u6587\u4ef6\u4e0a\u4f20\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\/194052"}],"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=194052"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194052\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194052"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}