最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺析Java基于Socket的文件傳輸案例

 更新時間:2016年02月10日 21:29:09   投稿:lijiao  
這篇文章主要針對Java基于Socket的文件傳輸案例進行詳細解析,具有一定的參考價值,感興趣的朋友可以參考一下

本文實例介紹了Java基于Socket的文件傳輸案例,分享給大家供大家參考,具體內(nèi)容如下

1、Java代碼

package com.wf.demo.socket.socketfile; 
 
import java.net.*; 
import java.io.*; 
 
/** 
 * 2.socket的Util輔助類 
 * 
 * @author willson 
 * 
 */ 
public class ClientSocket { 
 
  private String ip; 
 
  private int port; 
 
  private Socket socket = null; 
 
  DataOutputStream out = null; 
 
  DataInputStream getMessageStream = null; 
 
  public ClientSocket(String ip, int port) { 
    this.ip = ip; 
    this.port = port; 
  } 
 
  /** 
   * 創(chuàng)建socket連接 
   * 
   * @throws Exception 
   *       exception 
   */ 
  public void CreateConnection() throws Exception { 
 
    try { 
      socket = new Socket(ip, port); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      if (socket != null) 
        socket.close(); 
      throw e; 
    } finally { 
    } 
  } 
 
  // 發(fā)送消息 
  public void sendMessage(String sendMessage) throws Exception { 
    try { 
      out = new DataOutputStream(socket.getOutputStream()); 
      if (sendMessage.equals("Windows")) { 
        out.writeByte(0x1); 
        out.flush(); 
        return; 
      } 
      if (sendMessage.equals("Unix")) { 
        out.writeByte(0x2); 
        out.flush(); 
        return; 
      } 
      if (sendMessage.equals("Linux")) { 
        out.writeByte(0x3); 
        out.flush(); 
      } else { 
        out.writeUTF(sendMessage); 
        out.flush(); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
      if (out != null) 
        out.close(); 
      throw e; 
    } finally { 
    } 
  } 
 
  // 接受消息 
  public DataInputStream getMessageStream() throws Exception { 
    try { 
      getMessageStream = new DataInputStream(new BufferedInputStream( 
          socket.getInputStream())); 
      return getMessageStream; 
    } catch (Exception e) { 
      e.printStackTrace(); 
      if (getMessageStream != null) 
        getMessageStream.close(); 
      throw e; 
    } finally { 
    } 
  } 
 
  // 關閉連接 
  public void shutDownConnection() { 
    try { 
      if (out != null) 
        out.close(); 
      if (getMessageStream != null) 
        getMessageStream.close(); 
      if (socket != null) 
        socket.close(); 
    } catch (Exception e) { 
    } 
  } 
} 

2、Java代碼

package com.wf.demo.socket.socketfile; 
 
import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
/** 
 * 1.服務器端 
 * 
 * @author willson 
 * 
 */ 
public class ServerTest { 
   
  int port = 8821; 
 
  void start() { 
     
    Socket socket = null; 
     
    try { 
       
      ServerSocket serverSocket = new ServerSocket(port); 
       
      while (true) { 
        // 選擇進行傳輸?shù)奈募?
        String filePath = "E:\\lib.zip"; 
         
        File fi = new File(filePath); 
 
        System.out.println("File Name:" + fi.getName() + ";\tFile Size():" + (int) fi.length() + "bytes"); 
 
        // public Socket accept() throws 
        // IOException偵聽并接受到此套接字的連接。此方法在進行連接之前一直阻塞。 
 
         
        System.out.println("等待客戶端連接,連接端口:" + port); 
        socket = serverSocket.accept(); 
         
        System.out.println("建立socket鏈接"); 
         
        DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
         
        dis.readByte(); 
 
        DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); 
         
        DataOutputStream ps = new DataOutputStream(socket.getOutputStream()); 
         
        // 將文件名及長度傳給客戶端。這里要真正適用所有平臺,例如中文名的處理,還需要加工,具體可以參見Think In Java 
        // 4th里有現(xiàn)成的代碼。 
        ps.writeUTF(fi.getName()); 
        ps.flush(); 
        ps.writeLong((long) fi.length()); 
        ps.flush(); 
 
        int bufferSize = 8192; 
        byte[] buf = new byte[bufferSize]; 
 
        while (true) { 
           
          int read = 0; 
          if (fis != null) { 
            read = fis.read(buf); 
          } 
 
          if (read == -1) { 
            break; 
          } 
          ps.write(buf, 0, read); 
        } 
         
        ps.flush(); 
        // 注意關閉socket鏈接哦,不然客戶端會等待server的數(shù)據(jù)過來, 
        // 直到socket超時,導致數(shù)據(jù)不完整。 
        fis.close(); 
        socket.close(); 
         
        System.out.println("文件傳輸完成\n"); 
      } 
 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
 
  public static void main(String arg[]) { 
    new ServerTest().start(); 
  } 
} 
 
 


3、客戶端

package com.wf.demo.socket.socketfile; 
 
import java.io.BufferedOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.FileOutputStream; 
 
/** 
 * 3.客戶端 
 * 
 * @author willson 
 * 
 */ 
public class ClientTest { 
 
  private ClientSocket cs = null; 
 
  private String ip = "localhost";// 設置成服務器IP 
 
  private int port = 8821; 
 
  private String sendMessage = "Windwos"; 
 
  public ClientTest() { 
 
    try { 
      if (createConnection()) { 
        sendMessage(); 
        getMessage("F:\\"); 
      } 
    } catch (Exception ex) { 
      ex.printStackTrace(); 
    } 
  } 
 
  private boolean createConnection() { 
     
    cs = new ClientSocket(ip, port); 
    try { 
      cs.CreateConnection(); 
      System.out.print("連接服務器成功!" + "\n"); 
      return true; 
    } catch (Exception e) { 
      System.out.print("連接服務器失敗!" + "\n"); 
      return false; 
    } 
 
  } 
 
  private void sendMessage() { 
     
    if (cs == null) 
      return; 
    try { 
      cs.sendMessage(sendMessage); 
    } catch (Exception e) { 
      System.out.print("發(fā)送消息失敗!" + "\n"); 
    } 
  } 
 
  private void getMessage(String savePath) { 
     
    if (cs == null) 
      return; 
    DataInputStream inputStream = null; 
    try { 
      inputStream = cs.getMessageStream(); 
    } catch (Exception e) { 
      System.out.print("接收消息緩存錯誤\n"); 
      return; 
    } 
 
    try { 
       
      // 本地保存路徑,文件名會自動從服務器端繼承而來。 
      int bufferSize = 8192; 
      byte[] buf = new byte[bufferSize]; 
      int passedlen = 0; 
      long len = 0; 
 
      savePath += inputStream.readUTF(); 
      DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)))); 
      len = inputStream.readLong(); 
 
      System.out.println("File Size():" + len + "bytes"); 
      System.out.println("開始接收文件!" + "\n"); 
 
      while (true) { 
         
        int read = 0; 
        if (inputStream != null) { 
          read = inputStream.read(buf); 
        } 
        passedlen += read; 
        if (read == -1) { 
          break; 
        } 
        // 下面進度條本為圖形界面的prograssBar做的,這里如果是打文件,可能會重復打印出一些相同的百分比 
        System.out.println("文件接收了" + (passedlen * 100 / len) + "%\n"); 
        fileOut.write(buf, 0, read); 
      } 
      System.out.println("接收完成,文件存為" + savePath + "\n"); 
 
      fileOut.close(); 
    } catch (Exception e) { 
      System.out.println("接收消息錯誤" + "\n"); 
      return; 
    } 
  } 
 
  public static void main(String arg[]) { 
    new ClientTest(); 
  } 
} 

希望本文所述對大家學習java程序設計有所幫助。

相關文章

  • 如何對quartz定時任務設置結(jié)束時間

    如何對quartz定時任務設置結(jié)束時間

    這篇文章主要介紹了如何對quartz定時任務設置結(jié)束時間問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java異步調(diào)用轉(zhuǎn)同步方法實例詳解

    Java異步調(diào)用轉(zhuǎn)同步方法實例詳解

    這篇文章主要介紹了Java異步調(diào)用轉(zhuǎn)同步方法實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Java中的CopyOnWriteArrayList解析

    Java中的CopyOnWriteArrayList解析

    這篇文章主要介紹了Java中的CopyOnWriteArrayList解析,ArrayList是非線程安全的,也就是說在多個線程下進行讀寫,會出現(xiàn)異常,既然是非線程安全,那我們就使用一些機制把它變安全不就好了,需要的朋友可以參考下
    2023-12-12
  • SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer

    SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer

    這篇文章主要為大家介紹了SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • java遍歷Map的幾種方法分析

    java遍歷Map的幾種方法分析

    這篇文章主要介紹了java遍歷Map的幾種方法,結(jié)合實例形式分析了使用循環(huán)與迭代等方法操作Map遍歷的相關技巧,需要的朋友可以參考下
    2016-08-08
  • 詳細解讀java同步之synchronized解析

    詳細解讀java同步之synchronized解析

    synchronized關鍵字是Java里面最基本的同步手段,下面我們來一起學習一下
    2019-05-05
  • SPRINGMVC 406問題解決方案

    SPRINGMVC 406問題解決方案

    這篇文章主要介紹了SPRINGMVC 406問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • SpringBoot優(yōu)化連接數(shù)的方法詳解

    SpringBoot優(yōu)化連接數(shù)的方法詳解

    SpringBoot開發(fā)最大的好處是簡化配置,內(nèi)置了Tomcat,下面這篇文章主要給大家介紹了關于SpringBoot優(yōu)化連接數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Spring Boot Admin實踐詳解

    Spring Boot Admin實踐詳解

    在本篇文章里小編給大家整理了關于Spring Boot Admin實踐的相關知識點,有需要的朋友們可以學習下。
    2019-12-12
  • 基于ComponentScan注解的掃描范圍及源碼解析

    基于ComponentScan注解的掃描范圍及源碼解析

    這篇文章主要介紹了基于ComponentScan注解的掃描范圍及源碼解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論

新乐市| 汤原县| 丰都县| 区。| 宣恩县| 丰宁| 青冈县| 夹江县| 鞍山市| 醴陵市| 临泽县| 瑞金市| 南丹县| 漳浦县| 绵竹市| 平谷区| 旌德县| 万年县| 天全县| 洪雅县| 余庆县| 大邑县| 马鞍山市| 商水县| 全南县| 无锡市| 湖州市| 郓城县| 岳普湖县| 凌云县| 赤城县| 宝兴县| 磐石市| 丰台区| 万宁市| 双城市| 乌什县| 乐东| 井陉县| 洛南县| 长沙市|