JumpFly_W 2016-08-25 14:44 采纳率: 0%
浏览 1023

关于Socket文件传输后删除失败

文件通过socket传输后 想要删除本地的对应文件
通过File delete 失败, 在程序未关闭情况下 手动删除,提示 文件正在被使用,按理解来说,这应该是文件流没有关闭的情况,但是,我已经写了close 了,而且经过测试,在不改动代码的情况下,如果不经过socket传输, 文件是可以删除的。
但是经由socket以后,文件就无法删除,我也试了 deleteOnExit 代码if 测试 提示,删除成功,但是 查看文件夹 后 文件并没有被删除。

何解? Help ~!!

    private void TransFileToServer(String EnFilePath,OutputStream out) throws Exception{

        FileInputStream fileIn= new FileInputStream(EnFilePath);
        FileChannel fcIn=null;
        MappedByteBuffer mbbfIn=null;
        int len=0,outLenth;
        fcIn=fileIn.getChannel();
        mbbfIn=fcIn.map(FileChannel.MapMode.READ_ONLY, 0, fcIn.size());
        byte[] outBytes=new byte[1024];
        boolean more=true;
            while(more){
                len=mbbfIn.limit()-mbbfIn.position();
                if(len>outBytes.length)
                {   mbbfIn.get(outBytes, 0, outBytes.length);
                  out.write(outBytes);
                }else{
                    more=false;
                }
            } 
            if(len>0){mbbfIn.get(outBytes, 0, len);
                out.write(outBytes,0,len);
                 }
        if(fcIn!=null)
            fcIn.close();
        if(fileIn!=null)
            fileIn.close();
        if(out!=null)
            out.close();
        if(clientSocket!=null)
            clientSocket.close();
    }

代码正常的。 就是无法删除。 这些是放在一个 swingworker 线程里,也执行完毕了

  • 写回答

1条回答

  • threenewbee 2016-08-25 14:49
    关注

    你删除的代码呢?线程是异步执行的,删除的代码等待你发送结束才执行的么?

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示