File Transfer (OpcUaFile)
Overview
OPC UA Part 5 §10 FileType (i=11575) exposes 6 methods. The SDK wraps them as an OpcUaFile class, suitable for firmware upgrades and recipe transfers.
API
| Methods / Properties | Category | Reading and Writing | Note |
|---|---|---|---|
| new OpcUaFile(ua, fileNodeId) | Structure | — | Bind to FileType Instance |
| file.size() | Properties | Read | File size (long) |
| file.writable() | Properties | Read | Writable (boolean) |
| file.userWritable() | Properties | Read | Current user has write access (boolean) |
| file.openCount() | Properties | Read | Current Open Count (int) |
| file.open(mode) | Methods | Write | Open, OpcUaFile.Mode.* with the mode set to the byte flag |
| file.read(length) | Methods | Read | Read N bytes, then return Map.Entry<Enums.StatusCode, byte[]> |
| file.write(bytes) | Methods | Write | Write, Returns Enums.StatusCode |
| file.closeFile() | Methods | Write | Call FileType.Close to close the handle |
| file.close() | Methods | Write | AutoCloseable.close(), internal call to closeFile() |
| file.downloadTo(localPath, progress) | High-level | Read | One-click download; if localPath is Path or String, return Enums.StatusCode |
| file.uploadFrom(localPath, progress) | High-level | Write | One-click upload; if localPath is Path or String, return Enums.StatusCode |
Related Structures:
OpcUaFile.Mode isStatic Constant Class (not an enum), 4 byte flag bits can be combined using bitwise OR (Part 5 §10.4.1):
public static final class Mode {
public static final byte READ = 0x01; // Read
public static final byte WRITE = 0x02; // Write
public static final byte ERASE_EXISTING = 0x04; // Clear
public static final byte APPEND = 0x08; // Add
}
Example: f.open((byte) (OpcUaFile.Mode.WRITE | OpcUaFile.Mode.ERASE_EXISTING));
Code Example
import com.darra.opcua.*;
import java.util.Map;
import java.util.function.BiConsumer;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://device:4840")) {
ua.connect();
// 1) One-click firmware download (localPath using a string or java.nio.file.Path)
try (OpcUaFile file = new OpcUaFile(ua, "ns=2;s=Firmware.Image")) {
System.out.println("Size = " + file.size());
BiConsumer<Long, Long> progress = (done, total) ->
System.out.printf("\r%d%% (%d/%d)", done * 100 / total, done, total);
Enums.StatusCode rc = file.downloadTo("C:/dl/firmware.bin", progress);
if (rc != Enums.StatusCode.Good)
System.out.println("\n下载失败: " + rc);
}
// 2) Upload recipes with one click
try (OpcUaFile f2 = new OpcUaFile(ua, "ns=2;s=Recipe.Current")) {
f2.uploadFrom("C:/recipes/new.csv", null);
}
// 3) Manual streaming read
try (OpcUaFile f3 = new OpcUaFile(ua, "ns=2;s=Logs.Current")) {
f3.open(OpcUaFile.Mode.READ);
while (true) {
Map.Entry<Enums.StatusCode, byte[]> r = f3.read(4096);
byte[] chunk = r.getValue();
if (r.getKey() != Enums.StatusCode.Good || chunk == null || chunk.length == 0) break;
System.out.println("chunk " + chunk.length + " bytes");
}
f3.closeFile();
}
}
Implementation Notes
- Default block size 4 KB
- ByteString Walk ISO-8859-1 String concatenation, lossless preservation 0x00-0xFF
- Method NodeId Analysis: Priority TranslateBrowsePaths, fallback at the class level NodeId
Best Practices
- Always use
try-with-resources; failing to close it will cause a server handle leak. - Retrying in Segments for Large Files
- Do not access the same file handle concurrently
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_* |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
The C-side handle family consists of DarraUa_OpcUaFile_*: _New / _Free / _Open / _Close / _Read / _Write / _GetPosition / _SetPosition / _Size / _OpenCount / _Writable / _UserWritable / _IsOpen, and there is also a convenient entry point for the entire file: / DarraUa_OpcUaFile_DownloadTo _UploadFrom.