Corda中的附件:

时间:2017-12-05 15:49:28

标签: blockchain corda

我尝试使用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")
}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我在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")