在java中使用TCP通过字节数组传输二进制文件

发布于 2025-01-03 13:50:54 字数 1755 浏览 1 评论 0原文

我正在尝试一次按字节块将二进制文件从服务器传输到客户端。然而,我遇到了一个问题,它卡在传输 8kb 上。该文件通常大于 1mb,字节数组的大小为 1024。我相信它必须对我的 while 循环执行某些操作,因为它不会关闭我的连接。有什么帮助吗?感谢

客户端

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

public class FileClient {

    public static void main(String[] argv) throws IOException {
        Socket sock = new Socket("localhost", 4444);
        InputStream is = null;
        FileOutputStream fos = null;

        byte[] mybytearray = new byte[1024];
        try {
            is = sock.getInputStream();
            fos = new FileOutputStream("myfile.pdf");

            int count;
            while ((count = is.read(mybytearray)) >= 0) {
                fos.write(mybytearray, 0, count);
            }
        } finally {
            fos.close();
            is.close();
            sock.close();
        }
    }
}

服务器

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

public class FileServer {

    public static void main(String[] args) throws IOException {
        ServerSocket servsock = new ServerSocket(4444);
        File myFile = new File("myfile.pdf");
        FileInputStream fis = null;
        OutputStream os = null;
        while (true) {
            Socket sock = servsock.accept();
            try {
            byte[] mybytearray = new byte[1024];
            fis = new FileInputStream(myFile);
            os = sock.getOutputStream();

            int count;
            while ((count = fis.read(mybytearray)) >= 0) {
                os.write(mybytearray, 0, count);

            }
            os.flush();
            } finally {
            fis.close();
            os.close();
            sock.close();

            System.out.println("Socket closed");
            }
        }
    }
}

I am trying to transfer a binary file from the server to the client by blocks of bytes at a time. However, I am having a issue where it is stuck at transfering 8kb. The file is usually greater than a 1mb and the byte array is of size 1024. I believe it has to do something with my while loop since it doesnt close my connection. Any help? Thanks

Client

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

public class FileClient {

    public static void main(String[] argv) throws IOException {
        Socket sock = new Socket("localhost", 4444);
        InputStream is = null;
        FileOutputStream fos = null;

        byte[] mybytearray = new byte[1024];
        try {
            is = sock.getInputStream();
            fos = new FileOutputStream("myfile.pdf");

            int count;
            while ((count = is.read(mybytearray)) >= 0) {
                fos.write(mybytearray, 0, count);
            }
        } finally {
            fos.close();
            is.close();
            sock.close();
        }
    }
}

Server

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

public class FileServer {

    public static void main(String[] args) throws IOException {
        ServerSocket servsock = new ServerSocket(4444);
        File myFile = new File("myfile.pdf");
        FileInputStream fis = null;
        OutputStream os = null;
        while (true) {
            Socket sock = servsock.accept();
            try {
            byte[] mybytearray = new byte[1024];
            fis = new FileInputStream(myFile);
            os = sock.getOutputStream();

            int count;
            while ((count = fis.read(mybytearray)) >= 0) {
                os.write(mybytearray, 0, count);

            }
            os.flush();
            } finally {
            fis.close();
            os.close();
            sock.close();

            System.out.println("Socket closed");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2025-01-10 13:50:54

您的循环应该检查 count >= 0 而不是 count >= 0 0,并且流和套接字应该在finally块中关闭。除此之外,代码对我来说看起来不错。

“它卡在传输 8kb 上”是什么意思?有什么异常发生吗?

Your loops should check for count >= 0 rather than count > 0, and the streams and the socket should be closed in a finally block. Other than that, the code looks fine to me.

What do you mean by "it is stuck at transfering 8kb"? Any exception occurring?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文