java - 关于关闭 BufferedOutputStream

标签 java sockets socket.io serversocket

我正在尝试使用 TCP 开发一个简单的 Java 文件传输应用程序。 我当前的服务器代码如下:

package tcp.ftp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class FTPServer {

    public static void main(String[] args) {
        new FTPServer().go();
    }

    void go() {
        try {
            ServerSocket server = new ServerSocket(2015);
            System.out.println("server is running ....!");
            while (true) {
                Socket socket = server.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String file = reader.readLine();
                System.out.println("file to be downloaded is : " + file);
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
                while (true) {
                    int octet = bis.read();
                    if (octet == -1) {
                        break;
                    }
                    bos.write(octet);
                }
                 bos.flush();
                //bos.close();

            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

}

使用上面我当前的服务器代码,下载无法按预期工作。上面的代码将文件的一部分发送到客户端,而不是整个文件。请注意,我使用了flush方法来刷新缓冲区。但是当我用close()方法替换flush()方法时,文件被完全发送到客户端,没有任何损失。谁能解释一下这种行为!

更新:这是我客户的代码:

package tcp.ftp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author aaa
 */
public class FTPClient {

    public static void main(String[] args) {
        String file = "JasperReports-Ultimate-Guide-3.pdf";
        try {
            InetAddress address = InetAddress.getLocalHost();
            Socket socket = new Socket(address, 2015);
            System.out.println("connection successfully established ....!");
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            pw.println(file);
            pw.flush();
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy" + file));
            while (true) {
                int octet = bis.read();
                if (octet == -1) {
                    break;
                }
                bos.write(octet);
            }
            bos.flush();
            System.out.println("file download is complete ...!");

        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

不使用 Socket 的另一种行为。使用以下代码将文件从源复制到目标:

    public class CopieFile {
     static void fastCopy(String source, String destination) {

    try {
        FileInputStream fis = new FileInputStream(source);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(destination);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        while (true) {
            int octet = bis.read();
            if (octet == -1) {
                break;
            }
            bos.write(octet);
        }
        bos.flush();
    } catch (FileNotFoundException ex) {
        System.out.println(ex.getMessage());
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}
public static void main(String[] args) throws IOException {
        String source = "...";
        String destination = "...";
        fastCopy(source, destination);
}// end main
}// end class

上面的代码将文件从一个位置复制到另一个位置而没有任何丢失。请注意,我没有关闭流。

最佳答案

如果您从不关闭流,客户端将永远不会结束流,因此它永远不会退出读取循环。

无论如何,流和套接字都将超出范围,因此如果不关闭它们,就会发生资源泄漏。

关于java - 关于关闭 BufferedOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29631215/

相关文章:

java - java中如何将矩阵转换为图像

socket.io - Socket.IO 1.0 中向特定 ID 发送消息

javascript - 使用 jwt 和 socket io 的身份验证流程

node.js - NodeJS Socket.io Server<-->服务器通信

java - Docmosis pdf 转换返回已转换但为空的模板

java - 如果 Java 中的比较未命中

java - String[] 的问题

linux - 当在另一个线程上更改 pollfd 时,Linux 和 OS X 之间 poll() 的差异

c# - UWP(通用 Windows 平台)上的套接字通信

Linux 应用程序在没有套接字的情况下发送 UDP