我已经上传了一个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
}
答案 0 :(得分:1)
这是一个简单的端点,可以下载附件作为输入流:
@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)
}
}
}