欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用Socket通信傳輸文件的方法示例

 更新時(shí)間:2017年06月22日 09:55:52   作者:kongxx  
這篇文章主要介紹了Java使用Socket通信傳輸文件的方法,結(jié)合實(shí)例形式分析了java socket編程實(shí)現(xiàn)文件傳輸操作的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java使用Socket通信傳輸文件的方法。分享給大家供大家參考,具體如下:

前面幾篇文章介紹了使用Java的Socket編程和NIO包在Socket中的應(yīng)用,這篇文章說(shuō)說(shuō)怎樣利用Socket編程來(lái)實(shí)現(xiàn)簡(jiǎn)單的文件傳輸。

這里由于前面一片文章介紹了NIO在Socket中的應(yīng)用,所以這里在讀寫(xiě)文件的時(shí)候也繼續(xù)使用NIO包,所以代碼看起來(lái)會(huì)比直接使用流的方式稍微復(fù)雜一點(diǎn)點(diǎn)。

下面的示例演示了客戶(hù)端向服務(wù)器端發(fā)送一個(gè)文件,服務(wù)器作為響應(yīng)給客戶(hù)端回發(fā)一個(gè)文件。這里準(zhǔn)備兩個(gè)文件E:/test/server_send.log和E:/test/client.send.log文件,在測(cè)試完畢后在客戶(hù)端和服務(wù)器相同目錄下會(huì)多出兩個(gè)文件E:/test/server_receive.log和E:/test/client.receive.log文件。

下面首先來(lái)看看Server類(lèi),主要關(guān)注其中的sendFile和receiveFile方法。

package com.googlecode.garbagecan.test.socket.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyServer4 {
  private final static Logger logger = Logger.getLogger(MyServer4.class.getName());
  public static void main(String[] args) {
    Selector selector = null;
    ServerSocketChannel serverSocketChannel = null;
    try {
      // Selector for incoming time requests
      selector = Selector.open();
      // Create a new server socket and set to non blocking mode
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.configureBlocking(false);
      // Bind the server socket to the local host and port
      serverSocketChannel.socket().setReuseAddress(true);
      serverSocketChannel.socket().bind(new InetSocketAddress(10000));
      // Register accepts on the server socket with the selector. This
      // step tells the selector that the socket wants to be put on the
      // ready list when accept operations occur, so allowing multiplexed
      // non-blocking I/O to take place.
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
      // Here's where everything happens. The select method will
      // return when any operations registered above have occurred, the
      // thread has been interrupted, etc.
      while (selector.select() > 0) {
        // Someone is ready for I/O, get the ready keys
        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        // Walk through the ready keys collection and process date requests.
        while (it.hasNext()) {
          SelectionKey readyKey = it.next();
          it.remove();
          // The key indexes into the selector so you
          // can retrieve the socket that's ready for I/O
          doit((ServerSocketChannel) readyKey.channel());
        }
      }
    } catch (ClosedChannelException ex) {
      logger.log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      logger.log(Level.SEVERE, null, ex);
    } finally {
      try {
        selector.close();
      } catch(Exception ex) {}
      try {
        serverSocketChannel.close();
      } catch(Exception ex) {}
    }
  }
  private static void doit(final ServerSocketChannel serverSocketChannel) throws IOException {
    SocketChannel socketChannel = null;
    try {
      socketChannel = serverSocketChannel.accept();
      receiveFile(socketChannel, new File("E:/test/server_receive.log"));
      sendFile(socketChannel, new File("E:/test/server_send.log"));
    } finally {
      try {
        socketChannel.close();
      } catch(Exception ex) {}
    }
  }
  private static void receiveFile(SocketChannel socketChannel, File file) throws IOException {
    FileOutputStream fos = null;
    FileChannel channel = null;
    try {
      fos = new FileOutputStream(file);
      channel = fos.getChannel();
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      int size = 0;
      while ((size = socketChannel.read(buffer)) != -1) {
        buffer.flip();
        if (size > 0) {
          buffer.limit(size);
          channel.write(buffer);
          buffer.clear();
        }
      }
    } finally {
      try {
        channel.close();
      } catch(Exception ex) {}
      try {
        fos.close();
      } catch(Exception ex) {}
    }
  }
  private static void sendFile(SocketChannel socketChannel, File file) throws IOException {
    FileInputStream fis = null;
    FileChannel channel = null;
    try {
      fis = new FileInputStream(file);
      channel = fis.getChannel();
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      int size = 0;
      while ((size = channel.read(buffer)) != -1) {
        buffer.rewind();
        buffer.limit(size);
        socketChannel.write(buffer);
        buffer.clear();
      }
      socketChannel.socket().shutdownOutput();
    } finally {
      try {
        channel.close();
      } catch(Exception ex) {}
      try {
        fis.close();
      } catch(Exception ex) {}
    }
  }
}

下面是Client程序代碼,也主要關(guān)注sendFile和receiveFile方法

package com.googlecode.garbagecan.test.socket.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyClient4 {
  private final static Logger logger = Logger.getLogger(MyClient4.class.getName());
  public static void main(String[] args) throws Exception {
    new Thread(new MyRunnable()).start();
  }
  private static final class MyRunnable implements Runnable {
    public void run() {
      SocketChannel socketChannel = null;
      try {
        socketChannel = SocketChannel.open();
        SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
        socketChannel.connect(socketAddress);
        sendFile(socketChannel, new File("E:/test/client_send.log"));
        receiveFile(socketChannel, new File("E:/test/client_receive.log"));
      } catch (Exception ex) {
        logger.log(Level.SEVERE, null, ex);
      } finally {
        try {
          socketChannel.close();
        } catch(Exception ex) {}
      }
    }
    private void sendFile(SocketChannel socketChannel, File file) throws IOException {
      FileInputStream fis = null;
      FileChannel channel = null;
      try {
        fis = new FileInputStream(file);
        channel = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        int size = 0;
        while ((size = channel.read(buffer)) != -1) {
          buffer.rewind();
          buffer.limit(size);
          socketChannel.write(buffer);
          buffer.clear();
        }
        socketChannel.socket().shutdownOutput();
      } finally {
        try {
          channel.close();
        } catch(Exception ex) {}
        try {
          fis.close();
        } catch(Exception ex) {}
      }
    }
    private void receiveFile(SocketChannel socketChannel, File file) throws IOException {
      FileOutputStream fos = null;
      FileChannel channel = null;
      try {
        fos = new FileOutputStream(file);
        channel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        int size = 0;
        while ((size = socketChannel.read(buffer)) != -1) {
          buffer.flip();
          if (size > 0) {
            buffer.limit(size);
            channel.write(buffer);
            buffer.clear();
          }
        }
      } finally {
        try {
          channel.close();
        } catch(Exception ex) {}
        try {
          fos.close();
        } catch(Exception ex) {}
      }
    }
  }
}

首先運(yùn)行MyServer4類(lèi)啟動(dòng)監(jiān)聽(tīng),然后運(yùn)行MyClient4類(lèi)來(lái)向服務(wù)器發(fā)送文件以及接受服務(wù)器響應(yīng)文件。運(yùn)行完后,分別檢查服務(wù)器和客戶(hù)端接收到的文件。

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Spring MVC的參數(shù)綁定和返回值問(wèn)題

    Spring MVC的參數(shù)綁定和返回值問(wèn)題

    這篇文章主要介紹了Spring MVC的參數(shù)綁定和返回值問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案

    Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案

    這篇文章主要介紹了Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot3.x版本與Mybatis-Plus不兼容問(wèn)題

    SpringBoot3.x版本與Mybatis-Plus不兼容問(wèn)題

    當(dāng)使用3.x版本的SpringBoot結(jié)合Mybatis-Plus時(shí)版本不兼容就會(huì)報(bào)錯(cuò),本文就來(lái)介紹一下這個(gè)問(wèn)題的解決方法,感興趣的可以了解一下
    2024-03-03
  • SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路

    SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路

    這篇文章主要介紹了SpringBoot如何自定義starter啟動(dòng)器,通過(guò)starter的自定義過(guò)程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解,需要的朋友可以參考下
    2022-10-10
  • TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例

    TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例,簡(jiǎn)單介紹了tfidf算法及其相應(yīng)公式,然后分享了Java實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • springboot整合mybatis實(shí)現(xiàn)簡(jiǎn)單的一對(duì)多級(jí)聯(lián)查詢(xún)功能

    springboot整合mybatis實(shí)現(xiàn)簡(jiǎn)單的一對(duì)多級(jí)聯(lián)查詢(xún)功能

    這篇文章主要介紹了springboot整合mybatis實(shí)現(xiàn)簡(jiǎn)單的一對(duì)多級(jí)聯(lián)查詢(xún)功能,分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 詳細(xì)解讀AbstractStringBuilder類(lèi)源碼

    詳細(xì)解讀AbstractStringBuilder類(lèi)源碼

    這篇文章主要介紹了詳細(xì)解讀AbstractStringBuilder類(lèi)源碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • 關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等)

    關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等)

    這篇文章主要介紹了關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java中日期格式化YYYY-DD的操作bug

    Java中日期格式化YYYY-DD的操作bug

    這篇文章主要介紹了Java中日期格式化YYYY-DD中遇到的坑,本文給大家分享到腳本之家平臺(tái),需要的朋友可以參考下
    2020-01-01
  • Rabbitmq消息推送功能實(shí)現(xiàn)示例

    Rabbitmq消息推送功能實(shí)現(xiàn)示例

    rabbitMQ為異步消息處理提出了一個(gè)很好的解決方案,它是一個(gè)非常好用的消息中間件。主要解決當(dāng)生產(chǎn)者大量產(chǎn)生數(shù)據(jù)時(shí),消費(fèi)者無(wú)法快速消費(fèi)的問(wèn)題。這個(gè)時(shí)候需要一個(gè)中間層,保存這個(gè)數(shù)據(jù),rabbitMQ是一個(gè)很好的解決方案
    2022-12-12

最新評(píng)論