Java使用Socket通信传输文件的方法示例

 更新时间:2017年06月22日 09:55:52   作者:kongxx  
这篇文章主要介绍了Java使用Socket通信传输文件的方法,结合实例形式分析了java socket编程实现文件传输操作的相关技巧,需要的朋友可以参考下

本文实例讲述了Java使用Socket通信传输文件的方法。分享给大家供大家参考,具体如下:

前面几篇文章介绍了使用Java的Socket编程和NIO包在Socket中的应用,这篇文章说说怎样利用Socket编程来实现简单的文件传输。

这里由于前面一片文章介绍了NIO在Socket中的应用,所以这里在读写文件的时候也继续使用NIO包,所以代码看起来会比直接使用流的方式稍微复杂一点点。

下面的示例演示了客户端向服务器端发送一个文件,服务器作为响应给客户端回发一个文件。这里准备两个文件E:/test/server_send.log和E:/test/client.send.log文件,在测试完毕后在客户端和服务器相同目录下会多出两个文件E:/test/server_receive.log和E:/test/client.receive.log文件。

下面首先来看看Server类,主要关注其中的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程序代码,也主要关注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) {}
      }
    }
  }
}

首先运行MyServer4类启动监听,然后运行MyClient4类来向服务器发送文件以及接受服务器响应文件。运行完后,分别检查服务器和客户端接收到的文件。

更多关于java相关内容感兴趣的读者可查看本站专题:《Java Socket编程技巧总结》、《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • 解析整合mybatis-spring需要的maven依赖配置问题

    解析整合mybatis-spring需要的maven依赖配置问题

    这篇文章主要介绍了整合mybatis-spring需要的maven依赖配置问题,创建Maven项目,导入相关jar包,文中还给大家提到了,解决maven静态资源约定大于习惯问题,本文给大家介绍的非常详细,需要的朋友参考下吧
    2021-11-11
  • SpringBoot返回html界面的使用示例

    SpringBoot返回html界面的使用示例

    本文主要介绍了SpringBoot返回html界面的使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • SpringBoot返回Json对象报错(返回对象为空{})

    SpringBoot返回Json对象报错(返回对象为空{})

    本文主要介绍介绍了SpringBoot返回Json对象报错(返回对象为空{}),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • SpringMVC实现文件上传下载功能

    SpringMVC实现文件上传下载功能

    这篇文章主要为大家详细介绍了springMVC实现文件上传和下载的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • IDEA插件之快速删除Java代码中的注释

    IDEA插件之快速删除Java代码中的注释

    这篇文章主要介绍了IDEA插件之快速删除Java代码中的注释,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • 从Springboot项目中下载文件的具体过程

    从Springboot项目中下载文件的具体过程

    最近在做一个临时的项目,APP端在检测到程序有更新时,需要去后台下载新的安装包,接下来通过本文给大家分享从Springboot项目中下载文件的具体过程,感兴趣的朋友一起看看吧
    2021-07-07
  • Java API如何实现向Hive批量导入数据

    Java API如何实现向Hive批量导入数据

    这篇文章主要介绍了Java API如何实现向Hive批量导入数据的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java Swing组件下拉菜单控件JComboBox用法示例

    Java Swing组件下拉菜单控件JComboBox用法示例

    这篇文章主要介绍了Java Swing组件下拉菜单控件JComboBox用法,结合具体实例形式分析了Swing组件下拉菜单控件JComboBox的具体定义、使用方法及相关使用注意事项,需要的朋友可以参考下
    2017-11-11
  • java字符串常用操作方法(查找、截取、分割)

    java字符串常用操作方法(查找、截取、分割)

    今天小编就为大家分享一篇java字符串常用操作方法(查找、截取、分割),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Set接口深入剖析之HashSet、LinkedHashSet和TreeSet

    Set接口深入剖析之HashSet、LinkedHashSet和TreeSet

    这篇文章主要介绍了Set接口深入剖析之HashSet、LinkedHashSet和TreeSet,LinkedHashSet是HashSet的子类,实现了Set接口,LinkedHashSet底层是一个LinkedHashMap,底层维护了一个数组+双向链表,需要的朋友可以参考下
    2023-09-09

最新评论