Java 文件最后修改 returns 0

Java file last modified returns 0

我有以下代码来检查保存在网络驱动器上的文件的最后修改。

private long determineLastEdit(ILoaderData file) {

    String localDir = "c:\Software\log\"; 
    String localPDF = "empty28.pdf";
    String originDir = "smb:\ProdName\ShareName\Temp\username\path\to\folder\";
      //My company remote storage

    File localFile = new File(originDir + localPDF)
      //this does not work

    //File localFile = new File(localDir + localPDF)
      //this works as expected

    Date currentTime = new Date();
    long timeCurrent = currentTime.getTime();
    long timeFile = localFile.lastModified();
      //this returns 0 on remote, correct time on local

    boolean fileEx = localFile.isFile(); //returns false on remote, true on local
    boolean fileTp = localFile.isAbsolute(); //returns false on remote, true on local


    long difference = Math.abs(timeCurrent - timeFile);

    return difference;

}

文件构造函数的参数如下:

smb:\\ProdName\\ShareName\\Temp\\username\\path\\to\\folder\\empty28.pdf

但是,由于某种原因,lastModified() 方法 returns0,我做错了什么?该文件没有任何类型的锁定,它是常规文件(虽然是空 PDF)。

EDIT1: 我在本地文件上测试了这个方法,路径是:

c:\\Software\\log\\empty28.pdf

并且返回的值是正确的,我怀疑该方法不允许在给定文件上执行,因为它位于网络驱动器上。但是,此检查发生在一个已被授权的线程上。不知道错误在哪里。

EDIT2: 我更新了代码以提供更好的示例。现在,问题似乎出在从网络驱动器读取文件

EDIT3 我尝试使用不同的方法。导入:

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

并添加代码:

Path path = Paths.get(localDir + localPDF);
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);

同样的结果,本地驱动器工作,远程不工作。

就这么简单(注意:我包含了日期格式):

String localPDF = "empty28.pdf";
String originDir = "\\smb\ProdName\ShareName\Temp\username\path\to\file\";

File file = new File(originDir + localPDF);   
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

System.out.println(sdf.format(file.lastModified()));

根据Javadocs方法lastModified:

Returns a long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs.

因此,请检查您传递给文件构造函数的 URL。