问题
How to download the encrypted attachment in Corda? I have uploaded a file in corda and encrypted it and got the hash. How to download the same from other node?
回答1:
Consider, you have uploaded Jar as the attachment that contains a single file (e.g. legal agreement pdf), you can extract it as below:
//Get the attachmentJar from node for attachmentHash.
val attachmentJar: InputStream = cordaRPCOps.openAttachment(attachmentHash)
//Read the content of Jar to get file name and data.
var file_name_data: Pair<String, ByteArray>? = null
JarInputStream(attachment).use { jar ->
while (true) {
val nje = jar.nextEntry ?: break
if (nje.isDirectory) {
continue
}
file_name_data = Pair(nje.name, jar.readBytes())
}
}
回答2:
If an attachment is referenced by hash in a transaction that node A sends to node B, and node B has never seen the attachment corresponding to that hash, they will automatically request the attachment from node A and cache it locally for future reference.
回答3:
private fun downloadAttachment(proxy: CordaRPCOps, attachmentHash: SecureHash): JarInputStream {
val attachmentDownloadInputStream = proxy.openAttachment(attachmentHash)
return JarInputStream(attachmentDownloadInputStream)
}
来源:https://stackoverflow.com/questions/50966758/how-to-download-the-encrypted-attachment-in-corda