在java中使用socket发送文件

发布于 2025-01-10 02:22:46 字数 1493 浏览 1 评论 0原文

我想使用套接字将文件从客户端发送到服务器。

但在服务器上,我只收到第一行。问题是什么?

服务器代码

public static void main(String ars[]){
    int port = 1000;
    try {
        ServerSocket sercoc = new ServerSocket(port);
        System.out.println("dkjd");
        while (true){
            Socket soc = sercoc.accept();
            InputStream flux = soc.getInputStream();
            BufferedReader entree = new BufferedReader(new InputStreamReader(flux));
            String message = entree.readLine();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

客户端代码

public static void main(String args[]){
    String hote = "127.00.0.1";
    int port = 1000;
    FileReader input = null;
    
    File file = new File("src/view/files/temperature2.txt");
    Socket soc = null;
    try {
        soc = new Socket(hote,port);
        OutputStream flux = soc.getOutputStream();
        OutputStreamWriter sortie = new OutputStreamWriter(flux);
        try {
            input = new FileReader("src/view/files/temperature2.txt");

            char c;
            while ((c = (char) input.read()) != -1){
                sortie.write(c);

                //sortie.write("\n");
            }
        } finally {
            if (input != null){
                input.close();
            }
        }
      
        sortie.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如果我使用 BufferedReader 我发现错误并且我无法接收任何内容!

I want to send file from client to server using socket.

But at the server, I only receive the first line. What is the problem?

Server Code

public static void main(String ars[]){
    int port = 1000;
    try {
        ServerSocket sercoc = new ServerSocket(port);
        System.out.println("dkjd");
        while (true){
            Socket soc = sercoc.accept();
            InputStream flux = soc.getInputStream();
            BufferedReader entree = new BufferedReader(new InputStreamReader(flux));
            String message = entree.readLine();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Client Code

public static void main(String args[]){
    String hote = "127.00.0.1";
    int port = 1000;
    FileReader input = null;
    
    File file = new File("src/view/files/temperature2.txt");
    Socket soc = null;
    try {
        soc = new Socket(hote,port);
        OutputStream flux = soc.getOutputStream();
        OutputStreamWriter sortie = new OutputStreamWriter(flux);
        try {
            input = new FileReader("src/view/files/temperature2.txt");

            char c;
            while ((c = (char) input.read()) != -1){
                sortie.write(c);

                //sortie.write("\n");
            }
        } finally {
            if (input != null){
                input.close();
            }
        }
      
        sortie.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

If I use BufferedReader I found errors and I can't receive anything!

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

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

发布评论

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

评论(1

一个人的旅程 2025-01-17 02:22:46

在我的代码中,我通过ObjectOutputStream和ObjectInputStream实现了照片、txt和zip文件的传输。

代码:

// output
ObjectOutputStream objectOutput = new ObjectOutputStream(connection.clientSocket.getOutputStream());
                     objectOutput.writeObject(your_file);


// input
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                        buffer = (byte[]) ois.readObject();

in my code I have implemented the transfer of photo, txt and zip files through ObjectOutputStream and ObjectInputStream.

code:

// output
ObjectOutputStream objectOutput = new ObjectOutputStream(connection.clientSocket.getOutputStream());
                     objectOutput.writeObject(your_file);


// input
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                        buffer = (byte[]) ois.readObject();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文