博客
关于我
IO流 之 文件上传
阅读量:267 次
发布时间:2019-03-01

本文共 3107 字,大约阅读时间需要 10 分钟。

Java文件传输客户端实现

1.客户端代码解析

package com.tencent.network;import java.io.*;import java.net.InetAddress;import java.net.Socket;public class FileClient {    public static void main(String[] args) {        try {            // 创建socket连接            Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);                        // 获取输出流            OutputStream os = socket.getOutputStream();                        // 获取文件输入流            FileInputStream fs = new FileInputStream(new File("./file.out"));                        // 定义缓冲区            byte[] buffer = new byte[1024];            int len = 0;                        // 发送文件            while ((len = fs.read(buffer)) != -1) {                os.write(buffer, 0, len);            }                        // 通知服务器传输完成            socket.shutdownOutput();                        // 接收服务器回复            InputStream is = socket.getInputStream();            ByteArrayOutputStream bo = new ByteArrayOutputStream();            byte[] buffer2 = new byte[1024];            int len2 = 0;                        while ((len2 = is.read(buffer2)) != -1) {                bo.write(buffer2, 0, len2);            }                        // 输出服务器回复内容            System.out.println(bo.toString());                        // 关闭资源            fs.close();            os.close();            socket.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

2.服务器端代码解析

package com.tencent.network;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class FileServer {    public static void main(String[] args) {        try {            // 创建服务器socket            ServerSocket serverSocket = new ServerSocket(9000);                        // 等待客户端连接            Socket socket = serverSocket.accept();                        // 获取输入流            InputStream is = socket.getInputStream();                        // 定义输出流            OutputStream os = socket.getOutputStream();                        // 获取文件输入流            FileOutputStream fos = new FileOutputStream(new File("new"));                        // 接收文件            byte[] buffer = new byte[1024];            int len = 0;                        while ((len = is.read(buffer)) != -1) {                fos.write(buffer, 0, len);            }                        // 启动服务器输出流            socket.getOutputStream().write("我接收完毕了,你可以断开了".getBytes());                        // 关闭资源            fos.close();            is.close();            socket.close();            serverSocket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

文件传输原理与实现

在上述代码中,客户端通过Java Socket库连接到服务器,使用InputStream和OutputStream完成文件传输。以下是传输过程的详细说明:

  • 客户端

    • 创建Socket连接到指定IP地址和端口
    • 获取输出流,准备发送文件
    • 使用FileInputStream读取本地文件
    • 缓冲区读取文件内容并写入输出流
    • 通知服务器传输完成
    • 读取服务器回复并输出
  • 服务器端

    • 创建ServerSocket监听端口
    • 等待客户端连接
    • 获取输入流读取文件
    • 使用FileOutputStream将文件保存到指定路径
    • 启动输出流发送确认消息
    • 关闭所有资源
  • 实现特点

    • 双向通信:客户端发送文件后,服务器返回确认消息
    • 数据缓冲:使用了1024字节缓冲区,提高传输效率
    • 资源管理:严格按照try-catch结构关闭资源,避免资源泄漏
    • 异常处理:所有可能的IOException都有异常处理,确保程序健壮性

    总结

    上述代码实现了一个简单的文件传输协议,适用于内部网络传输场景。通过Socket库的双向通信机制,实现了高效的文件传输功能。客户端和服务器端分别负责文件的读取和写入,整个过程保持低耦合,易于扩展和维护。

    转载地址:http://njpt.baihongyu.com/

    你可能感兴趣的文章
    Python2 安装MySQLdb库
    查看>>
    Python2 已终结,入手Python 3,你需要这30个技巧
    查看>>
    python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
    查看>>
    python2.7安装
    查看>>
    Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
    查看>>
    PYTHON2.Git.1
    查看>>
    python20181211
    查看>>
    Python内存管理机制
    查看>>
    Python2中range 和xrange的区别??
    查看>>
    python2和3的区别
    查看>>
    python2和python3的pip版本升级
    查看>>
    python2和python3的区别
    查看>>
    python2备份交换机配置脚本
    查看>>
    Python3 - 在字符串格式化程序参数中使用变量
    查看>>
    Python3 A*寻路算法实现
    查看>>
    python3 grpc安装
    查看>>
    python3 mysql 封装多表_python3 mysql-多表查询
    查看>>
    python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
    查看>>
    Python关键代码总览!从Zero到Hero必看的考点
    查看>>
    python3 robotframework+pycharm框架搭建
    查看>>