输入散列值后如何在corda中下载jar文件?
我试图上传一个zip文件并得到了散列。现在我调用了一个api。在这种情况下,我将提供散列来下载文件。但我无法下载该文件输入散列值后如何在corda中下载jar文件?,corda,Corda,我试图上传一个zip文件并得到了散列。现在我调用了一个api。在这种情况下,我将提供散列来下载文件。但我无法下载该文件 @PUT @Path("download_file") fun createIOU( @QueryParam("sechash") sechash: String ): Response { val SecuHash = SecureHash.parse(sechash) return try { val attachmentJ
@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) {
//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())
}
}
}
@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())
}
}
}
您可能想看看这个博客。它详细说明了如何在Corda中实现附件的上传和下载
它演示了使用两种不同方法下载附件:
使用attachmentId/hash
@GetMapping("/{hash}")
fun downloadByHash(@PathVariable hash: String): ResponseEntity<Resource> {
val inputStream = InputStreamResource(proxy.openAttachment(SecureHash.parse(hash)))
return ResponseEntity.ok().header(
HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"$hash.zip\""
).body(inputStream)
}
@GetMapping(“/{hash}”)
有趣的downloadByHash(@PathVariable hash:String):ResponseEntity{
val inputStream=InputStreamResource(proxy.openAttachment(SecureHash.parse(hash)))
返回ResponseEntity.ok().header(
HttpHeaders.CONTENT\u处置,
“附件;文件名=\”$hash.zip“”
).body(输入流)
}
使用附件的名称
@GetMapping
fun downloadByName(@RequestParam name: String): ResponseEntity<Resource> {
val attachmentIds: List<AttachmentId> = proxy.queryAttachments(
AttachmentQueryCriteria.AttachmentsQueryCriteria(filenameCondition = Builder.equal(name)),
null
)
val inputStreams = attachmentIds.map { proxy.openAttachment(it) }
val zipToReturn = if (inputStreams.size == 1) {
inputStreams.single()
} else {
combineZips(inputStreams, name)
}
return ResponseEntity.ok().header(
HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"$name.zip\""
).body(InputStreamResource(zipToReturn))
}
private fun combineZips(inputStreams: List<InputStream>, filename: String): InputStream {
val zipName = "$filename-${UUID.randomUUID()}.zip"
FileOutputStream(zipName).use { fileOutputStream ->
ZipOutputStream(fileOutputStream).use { zipOutputStream ->
inputStreams.forEachIndexed { index, inputStream ->
val zipEntry = ZipEntry("$filename-$index.zip")
zipOutputStream.putNextEntry(zipEntry)
inputStream.copyTo(zipOutputStream, 1024)
}
}
}
return try {
FileInputStream(zipName)
} finally {
Files.deleteIfExists(Paths.get(zipName))
}
}
@GetMapping
fun downloadByName(@RequestParam name:String):ResponseEntity{
val attachmentIds:List您可以检查节点的web日志并查看是否有堆栈跟踪吗?如果有,您可以发布它吗?downloadAttachment
函数应该向调用者返回file\u name\u data
值……因此修改fun定义,将返回类型添加为Pair
并在有趣。欢迎链接到某个解决方案,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面最相关的部分,以防目标页面不可用。