th_lo
th_lo

Reputation: 575

How to check filesize in Java without reading the file from an UNC Path?

I wonder how to check the file-size in Java without opening or reading the file via UNC Path from two different Microsoft Server? It is for an Interface Engine Filter Function. So no HTML DOM etc. is available.

What works so right now: There is an Util Function from the API

java.nio.file.Files.size(java.nio.file.Paths.get('/path/to/file')

which does not work on UNC PATHS due an Error:

Wrapped java.nio.file.AccessDeniedException: \\server\folder\file.pdf

Example for use:

var filePath = msg['path'].toString();
try{
    var fileContent = java.nio.file.Files.size(java.nio.file.Paths.get(filePath));
    var fileSize = fileContent / 1048576;
    logger.debug("Filesize in MB: " + fileSize);
}
catch(e){
...

Expect: A way to read the file size out file attribute via UNC access

Upvotes: 0

Views: 1334

Answers (1)

Shamil
Shamil

Reputation: 940

Since you know the file path, you always may use java.io.File.length() to return the length, in bytes, of the file. I.e.:

var filePath = msg['path'].toString();
// Validate that the filePath is indeed a file and it exists
var fileSize = Packages.java.io.File(filePath).length();

Upvotes: 2

Related Questions