Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java socket fileoutputstream可以';不要关闭文件_Java_Sockets_Netbeans_Fileoutputstream - Fatal编程技术网

java socket fileoutputstream可以';不要关闭文件

java socket fileoutputstream可以';不要关闭文件,java,sockets,netbeans,fileoutputstream,Java,Sockets,Netbeans,Fileoutputstream,我写了一个通过套接字传输文件的代码, 该文件已正确传输,但即使在调用.close()方法后也未关闭 但是,文件在关闭“套接字”后关闭,但我希望保持连接打开 这里,服务器将文件发送到客户端 服务器代码 public void sendFile(String sfileName) throws IOException{ try{ in = new FileInputStream(sfileName); out = socket.getOutputStream

我写了一个通过套接字传输文件的代码, 该文件已正确传输,但即使在调用
.close()
方法后也未关闭

但是,文件在关闭“套接字”后关闭,但我希望保持连接打开

这里,服务器将文件发送到客户端

服务器代码

public  void sendFile(String sfileName) throws IOException{
    try{
        in = new FileInputStream(sfileName);
        out = socket.getOutputStream();
        transferData(in,out);
    }
    finally {
        in.close();
        in = null;
        System.gc();
    }
}
private void transferData(InputStream in, OutputStream out) throws IOException  {
    byte[] buf = new byte[8192];
    int len = 0;
    while(in.available()==0);
    while ((len = in.read(buf)) != -1) {
    out.write(buf, 0, len);
    }
    out.flush();
}
客户端代码:

public  void recieveFile(String rfileName) throws IOException{
    try{
        in = socket.getInputStream();
        System.out.println("Reciever file : " + rfileName);
        out = new FileOutputStream(rfileName);
        transferData(in,out);
    }
    finally{
        out.flush();
        out.close();
        out = null;
        System.gc();
    }
}
private void transferData(InputStream in, OutputStream out) throws IOException  {
    byte[] buf = new byte[8192];
    int len = 0;
    while(in.available()==0);
    while ((len = in.read(buf)) != -1) {
    out.write(buf, 0, len);
    }
    out.flush();
}

代码有什么问题

我认为您应该使用

是,文件在Socket.shutdownOutput()之后关闭,但问题是我想保持“打开”连接以进行进一步的文件传输…您如何知道文件未关闭?更改是否未写入磁盘?文件无法删除、重命名等。。。但是文件大小还可以。但是,在关闭连接或终止程序后,它会工作。。。。我怀疑您的read()会阻塞,等待更多信息到达,因此它不会终止,文件也不会关闭。也许读取预设的字节数而不是等待-1?但即使文件也应该关闭,bcoz我已经将close()方法放入finally block…]当available()为零时旋转的循环实际上是浪费时间。移除它。以下读取将阻塞,而不是占用CPU。