Corda 获取给定哈希的文件

Corda 获取给定哈希的文件,corda,Corda,我上传了一个jar文件并找到了它的散列。现在尝试通过提供其哈希来下载该文件。运行以下代码时,输出为:- jar:(blacklist.txt[B@35f9985c) 我们可以从输出中推断出什么。如何查看下载的文件 @PUT @Path("download_file") fun createIOU( @QueryParam("sechash") sechash: String ): Response { val SecuHash

我上传了一个jar文件并找到了它的散列。现在尝试通过提供其哈希来下载该文件。运行以下代码时,输出为:-

jar:(blacklist.txt[B@35f9985c)

我们可以从输出中推断出什么。如何查看下载的文件

 @PUT
    @Path("download_file")
    fun createIOU(
            @QueryParam("sechash") sechash: String
    ): Response {

        val SecuHash = SecureHash.parse(sechash)
        return try {
            val attachmentJr = downloadAttachment(rpcOps, SecuHash)
            println("jar      :"+attachmentJr)
            Response.status(CREATED).entity("Transaction id ${attachmentJr} committed to ledger.\n").build()
        } catch (ex: Throwable) {
            logger.error(ex.message, ex)
            Response.status(BAD_REQUEST).entity(ex.message!!).build()
        }
    }

    private fun downloadAttachment(proxy: CordaRPCOps, attachmentHash: SecureHash): Pair<String, ByteArray>? {
        //Get the attachmentJar from node for attachmentHash.
        val attachmentJar = proxy.openAttachment(attachmentHash)
        //Read the content of Jar to get file name and data.
        var file_name_data: Pair<String, ByteArray>? = null
        JarInputStream(attachmentJar).use { jar ->
            while (true) {
                val nje = jar.nextEntry ?: break
                if (nje.isDirectory) {
                    continue
                }
                file_name_data = Pair(nje.name, jar.readBytes())

            }
        }
       return file_name_data
    }
@PUT
@路径(“下载文件”)
有趣的创意(
@QueryParam(“sechash”)sechash:String
):回应{
val SecuHash=SecureHash.parse(sechash)
回击{
val attachmentJr=下载附件(rpcOps,SecuHash)
println(“jar:+attachmentJr)
Response.status(CREATED).entity(“事务id${attachmentJr}已提交到分类账。\n”).build()
}捕获(例如:可丢弃){
记录器错误(例如消息,例如)
Response.status(请求错误).entity(例如message!!).build()
}
}
私人娱乐下载附件(代理:CordaRPCOps,附件:SecureHash):配对{
//从attachmentHash的节点获取attachmentJar。
val attachmentJar=proxy.openAttachment(attachmentHash)
//读取Jar的内容以获取文件名和数据。
var文件\u名称\u数据:对?=null
JarInputStream(attachmentJar)。使用{jar->
while(true){
val nje=jar.nextEntry?:中断
国际单项体育联合会(nje.isDirectory){
持续
}
file\u name\u data=Pair(nje.name,jar.readBytes())
}
}
返回文件名数据
}
获得的输出为:

这里有一个简单的端点,可以将附件下载为输入流:

@GET
@Path("download-attachment")
fun downloadAttachment(@QueryParam("attachment_hash") attachmentHash: String): InputStream {
    val secureHash = SecureHash.parse(attachmentHash)
    return rpcOps.openAttachment(secureHash)
}
然后,您需要将输入流作为JAR文件写入

fun downloadJar(url: String) {
    val inputStream = URL(url).openStream()

    BufferedReader(InputStreamReader(inputStream)).use {
        File("./downloaded_jar.jar").outputStream().use { outputStream ->
            inputStream.copyTo(outputStream)
        }
    }
}