关闭BufferedOutputStream

时间:2015-04-14 15:20:24

标签: 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());
        }
    }

}

使用上面的当前服务器代码,downlloding无法按预期工作。上面的代码将部分文件发送到客户端,而不是整个文件。请注意,我使用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

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

1 个答案:

答案 0 :(得分:2)

如果你从不关闭流,客户端永远不会得到流的结束,所以它永远不会退出读循环。

在任何情况下,流和套接字都将超出范围,因此如果您不关闭它们,则会导致资源泄漏。