Corda 中的附件:

Attachment in Corda :

我尝试使用 uploadAttachemnt 方法上传 zip 文件,我得到了一个 secureHash 作为输出。 我尝试使用哈希作为 openAttachmnet 方法的输入来下载相同的附件,我得到了一个 InputStream。 当我尝试使用 BuffeReader 读取 inputStream 的内容时,它被加密了。我意识到我必须解压缩文件并阅读它所以我得到了这个包 "import java.util.zip.ZipEntry" 来读取 zip 文件内容 我不确定是否可以使用 InputStream 读取 zip 文件内容。 我应该如何使用 InputStream 读取 zip 文件内容?如果不是,我应该解压缩并上传文件吗?

fun main(args: Array<String>) :String {
    require(args.isNotEmpty()) { "Usage: uploadBlacklist <node address>" }
    args.forEach { arg ->
        val nodeAddress = parse(args[0])
        val rpcConnection = CordaRPCClient(nodeAddress).start("user1", "test")
        val proxy = rpcConnection.proxy

         val attachmentInputStream = File(args[1]).inputStream()
        val attachmentHash = proxy.uploadAttachment(attachmentInputStream)
        print("AtachmentHash"+attachmentHash)


        // Download the attachment
        val inputString = proxy.openAttachment(attachmentHash).bufferedReader().use { it.readText() }
        println("The contents are ")
        print(inputString)

        val file = File("OutputFile.txt")
        file.writeText(inputString)
        rpcConnection.notifyServerAndClose()

    }

    return("File downloaded successfully in the path")
}

您需要将 openAttachment 返回的 InputStream 转换为 JarInputStream。然后,您可以使用 JarInputStream 的方法来定位条目并读取其内容:

val attachmentDownloadInputStream = proxy.openAttachment(attachmentHash)
val attachmentJar = JarInputStream(attachmentDownloadInputStream)

while (attachmentJar.nextEntry.name != expectedFileName) {
    attachmentJar.nextEntry
}

val contents = attachmentJar.bufferedReader().readLines()

举个例子,看看黑名单示例 CorDapp 的 RPC 客户端代码:https://github.com/corda/samples.

我在 zip 文件中有很多文件。所以我尝试了这段代码并且效果很好。感谢您的输入乔尔。

// downloading the attachment
        val attachmentDownloadInputStream = proxy.openAttachment(attachmentHash)
        val attachmentJar = JarInputStream(attachmentDownloadInputStream)
        var contents =""

        //Reading the contents
        while(attachmentJar.nextJarEntry!=null){
            contents = contents + attachmentJar.bufferedReader().readLine()

        }
        println("The contents are $contents")