温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Java Socket进行文件传输

发布时间:2025-09-28 01:17:48 来源:亿速云 阅读:95 作者:小樊 栏目:编程语言

使用Java Socket进行文件传输涉及客户端和服务器端的编程。以下是一个简单的示例,展示了如何使用Java Socket进行文件传输。

服务器端代码

import java.io.*;
import java.net.*;

public class FileServer {
    public static void main(String[] args) {
        int port = 12345; // 服务器端口
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("服务器已启动,等待客户端连接...");

            try (Socket socket = serverSocket.accept()) {
                System.out.println("客户端已连接");

                // 获取输入流,用于读取客户端发送的文件名
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                String fileName = dis.readUTF();
                System.out.println("接收文件: " + fileName);

                // 获取输出流,用于向客户端发送文件
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF("文件接收成功");

                // 打开文件输出流,准备写入文件
                FileOutputStream fos = new FileOutputStream(fileName);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                // 读取客户端发送的文件数据并写入本地文件
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = socket.getInputStream().read(buffer)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                }

                // 关闭流
                bos.close();
                fos.close();
                dis.close();
                dos.close();
                socket.close();
                serverSocket.close();

                System.out.println("文件传输完成");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端代码

import java.io.*;
import java.net.*;

public class FileClient {
    public static void main(String[] args) {
        String serverAddress = "localhost"; // 服务器地址
        int port = 12345; // 服务器端口
        String filePath = "example.txt"; // 要发送的文件路径

        try (Socket socket = new Socket(serverAddress, port)) {
            System.out.println("已连接到服务器");

            // 获取输出流,用于向服务器发送文件名
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeUTF(filePath);
            dos.flush();

            // 获取输入流,用于读取服务器的响应
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            String response = dis.readUTF();
            System.out.println(response);

            // 打开文件输入流,准备读取文件
            FileInputStream fis = new FileInputStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(fis);

            // 读取本地文件数据并发送给服务器
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bis.read(buffer)) != -1) {
                socket.getOutputStream().write(buffer, 0, bytesRead);
            }

            // 关闭流
            bis.close();
            fis.close();
            dos.close();
            dis.close();
            socket.close();

            System.out.println("文件传输完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行步骤

  1. 启动服务器端:运行FileServer类,服务器将监听指定端口(例如12345)。
  2. 启动客户端:运行FileClient类,客户端将连接到服务器并发送指定文件(例如example.txt)。

注意事项

  • 确保服务器和客户端在同一台机器上运行时,使用localhost作为服务器地址。
  • 如果服务器和客户端不在同一台机器上,需要将serverAddress设置为服务器的实际IP地址。
  • 文件传输过程中可能会遇到网络问题,建议添加异常处理和重试机制。
  • 为了提高效率,可以使用更大的缓冲区大小。

通过以上步骤,你可以使用Java Socket实现简单的文件传输功能。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI