夢想飛翔

          自強(qiáng)不息
          posts - 111, comments - 30, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          /** 
           * 發(fā)送端 
           */ 
          class Client {  
             
              // 網(wǎng)上抄來的,將 int 轉(zhuǎn)成字節(jié)  
              public static byte[] i2b(int i) {  
                  return new byte[]{  
                          (byte) ((i >> 24) & 0xFF),  
                          (byte) ((i >> 16) & 0xFF),  
                          (byte) ((i >> 8) & 0xFF),  
                          (byte) (i & 0xFF)  
                  };  
              }  
             
              /** 
               * 發(fā)送文件。文件大小不能大于 {@link Integer#MAX_VALUE} 
               * 
               * @param hostname 接收端主機(jī)名或 IP 地址 
               * @param port     接收端端口號 
               * @param filepath 文件路徑 
               * 
               * @throws IOException 如果讀取文件或發(fā)送失敗 
               */ 
              public void sendFile(String hostname, int port, String filepath) throws IOException {  
                  File file = new File(filepath);  
                  FileInputStream is = new FileInputStream(filepath);  
             
                  Socket socket = new Socket(hostname, port);  
                  OutputStream os = socket.getOutputStream();  
             
                  try {  
                      int length = (int) file.length();  
                      System.out.println("發(fā)送文件:" + file.getName() + ",長度:" + length);  
             
                      // 發(fā)送文件名和文件內(nèi)容  
                      writeFileName(file, os);  
                      writeFileContent(is, os, length);  
                  } finally {  
                      os.close();  
                      is.close();  
                  }  
              }  
             
              // 輸出文件內(nèi)容  
              private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {  
                  // 輸出文件長度  
                  os.write(i2b(length));  
             
                  // 輸出文件內(nèi)容  
                  byte[] buffer = new byte[4096];  
                  int size;  
                  while ((size = is.read(buffer)) != -1) {  
                      os.write(buffer, 0, size);  
                  }  
              }  
             
              // 輸出文件名  
              private void writeFileName(File file, OutputStream os) throws IOException {  
                  byte[] fn_bytes = file.getName().getBytes();  
             
                  os.write(i2b(fn_bytes.length));         // 輸出文件名長度  
                  os.write(fn_bytes);    // 輸出文件名  
              }  

          本文來自: IT知道網(wǎng)(http://www.itwis.com) 詳細(xì)出處參考:http://www.itwis.com/html/java/j2se/20090304/3503_2.html

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁都县| 阿拉尔市| 抚顺市| 文昌市| 灌阳县| 古田县| 平乡县| 新丰县| 晋州市| 莆田市| 桐梓县| 黑龙江省| 武安市| 鸡东县| 长寿区| 谢通门县| 吉隆县| 博罗县| 通城县| 大渡口区| 连平县| 樟树市| 宜春市| 三都| 津南区| 夏河县| 靖安县| 平安县| 阳信县| 通榆县| 临沧市| 丹东市| 隆子县| 招远市| 佛学| 阿城市| 长汀县| 兖州市| 湘阴县| 三穗县| 河津市|