Corda中的附件:

问题描述 投票:0回答:2

我尝试使用uploadAttachemnt方法上传zip文件,其中我将secureHash作为输出。我尝试使用hash作为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")
}
blockchain corda
2个回答
2
投票

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

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

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

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

例如,请在此处查看Blacklist示例CorDapp的RPC客户端代码:https://github.com/corda/samples


0
投票

我在zip文件中有很多文件。所以我尝试了这个代码,它工作正常。感谢您输入的joel。

// 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")
© www.soinside.com 2019 - 2024. All rights reserved.