夢想飛翔

          自強不息
          posts - 111, comments - 30, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java實現Socket發送和接收文件的代碼

          Posted on 2009-09-10 20:34 love1563 閱讀(1007) 評論(0)  編輯  收藏
          /** 
           * 發送端 
           */ 
          class Client {  
             
              // 網上抄來的,將 int 轉成字節  
              public static byte[] i2b(int i) {  
                  return new byte[]{  
                          (byte) ((i >> 24) & 0xFF),  
                          (byte) ((i >> 16) & 0xFF),  
                          (byte) ((i >> 8) & 0xFF),  
                          (byte) (i & 0xFF)  
                  };  
              }  
             
              /** 
               * 發送文件。文件大小不能大于 {@link Integer#MAX_VALUE} 
               * 
               * @param hostname 接收端主機名或 IP 地址 
               * @param port     接收端端口號 
               * @param filepath 文件路徑 
               * 
               * @throws IOException 如果讀取文件或發送失敗 
               */ 
              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("發送文件:" + file.getName() + ",長度:" + length);  
             
                      // 發送文件名和文件內容  
                      writeFileName(file, os);  
                      writeFileContent(is, os, length);  
                  } finally {  
                      os.close();  
                      is.close();  
                  }  
              }  
             
              // 輸出文件內容  
              private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {  
                  // 輸出文件長度  
                  os.write(i2b(length));  
             
                  // 輸出文件內容  
                  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知道網(http://www.itwis.com) 詳細出處參考:http://www.itwis.com/html/java/j2se/20090304/3503_2.html

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


          網站導航:
           
          主站蜘蛛池模板: 保山市| 甘孜| 通化县| 乡城县| 建昌县| 康马县| 朝阳县| 桃江县| 新丰县| 鹰潭市| 象山县| 上饶县| 宿州市| 遂川县| 巴彦县| 浦东新区| 石楼县| 页游| 平乐县| 武城县| 房产| 芦山县| 姜堰市| 平谷区| 青海省| 宣恩县| 林口县| 略阳县| 四子王旗| 黄平县| 平遥县| 屏南县| 栾川县| 游戏| 壶关县| 谷城县| 广元市| 三都| 伊通| 纳雍县| 布拖县|