【问题标题】:Does IOUtils.copy block until writing is finished?IOUtils.copy 是否会阻塞直到写入完成?
【发布时间】:2013-07-03 23:08:33
【问题描述】:

这是我的情况:我正在使用IOUtils 复制文件。接下来我要做的是向另一个程序发送一条 JSON 消息,告诉它“你可以下载副本”。问题是大约 25% 的时间其他程序收到错误消息“收到意外的 EOF 下载工件”。

每次出现这个错误,如果我手动重试,就不会出现这个错误。我的理论是IOUtils.copy 不会阻塞,操作系统仍在将文件写入 FS,而其他程序试图下载它。有没有办法强制IOUtils.copy 或其他功能等效的代码阻塞,直到操作系统完成写入文件?还是我的理论不正确?这是我正在使用的代码:

private boolean archiveArtifact(String archivePath, String deployId, Artifact artifact) {
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    boolean successful = true;

    try {
        File archiveDir = new File(archivePath);
        File deployDir = new File(archiveDir, deployId);

        if (!deployDir.exists()) {
            deployDir.mkdirs();
        }

        URLConnection connection = new URL(artifact.getJenkinsUrl()).openConnection();
        inputStream = connection.getInputStream();
        File output = new File(deployDir, artifact.getFileName());
        fileOutputStream = new FileOutputStream(output);
        IOUtils.copy(inputStream, fileOutputStream);
    } catch (IOException e) {
        successful = false;
        logger.error(e.getMessage(), e);
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            successful = false;
            logger.error(e.getMessage(), e);
        }

        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            successful = false;
            logger.error(e.getMessage(), e);
        }
    }

    return successful;
}

值得注意的是,我正在将其复制到 NFS。请记住,我对 NFS 真的一无所知。这是 CentOS 5.9 版(最终版)。

【问题讨论】:

    标签: java file filesystems apache-commons nfs


    【解决方案1】:

    您当前的代码仅确保将文件内容传递给操作系统进行写入;它并不能保证它实际上已写入磁盘。

    要确定文件是否实际写入磁盘,您可以在FileDescriptor 上调用sync()

    fileOutputStream.flush();
    fileOutputStream.getFD().sync();
    

    【讨论】:

    • 好的,谢谢,我会试试看问题是否消失。
    猜你喜欢
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2010-12-04
    • 1970-01-01
    • 2015-05-15
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多