文件寻找动态增加的文件

我试图在本地保存在线流,然后从我的本地节点分发流。 计划流程: 第一次请求url,url-test,创建一个编写器线程,该线程开始使用文件名url-file写入文件系统。对该URL的所有后续请求url-test都是从本地文件系统处理的。 作家线程
protected class Writer implements Runnable {
    String url;

    public void run() {
        FileOutputStream out_file = null;
        File cacheFile = new File("url-file");
        byte[] buf = new byte[4096];
        int count = 0;
        try {
            URL urlstream = new URL(url);
            // cv is an object which stores url information
            cv.originInputStream = urlstream.openStream();
            out_file = new FileOutputStream(cacheFile);
            while ((count = cv.originInputStream.read(buf)) > 0) {
                out_file.write(buf, 0, count);
                out_file.flush();
                cv.incrementTotalBytes(count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
现在,对于下一个请求,我需要读取本地保存的文件,url文件,并移动到其中的最后保存位置。我正在使用cv对象的totalBytes属性,它给出了编写器线程保存的总字节数。
FileInputStream in = new FileInputStream("url-file");
        response.setHeader("Content-Disposition", "inline; filename="
                + localFile.getName());
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "-1");

        OutputStream out = response.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[4096];
        int count = 0;
        FileChannel inc = in.getChannel();
        ByteBuffer b = ByteBuffer.allocate(4096);
        inc.position(cv.getTotalBytes());

        while ((count = inc.read(b)) >= 0) {
            out.write(b.array(), 0, count);
            b.clear();
        }
我没有看到任何输出,在文件中搜索的最佳方式是什么,这是由另一个线程更新的。 编辑:我希望编写器线程继续写入文件,任何请求的响应应从该时间实例开始。简而言之,在文件通道中设置位置时,我的编写器线程仍在写入文件。即使我将文件通道位置设置为低于25%,说
inc.position(totalBytes - (long) 0.25 * totalBytes)
我仍然看不到输出。     
已邀请:
在我看来,
cv
保存文件中的总字节数。每次将数据附加到文件时都会对ѭ3进行更新调用。 第二个片段似乎使用相同的值(“filesize”)作为文件通道中的标记。根据我的理解,你总是在文件的最后一个字节上设置标记并尝试从该位置开始阅读,显然,你会立即看到EOF。 重新思考计算响应起始位置的方法。它不应该是文件的最后一个字节。     

要回复问题请先登录注册