我尝试使用Hibernate 5.2.11.Final(BLOB类型)和Vert.x 3.4.2从数据库(Postgresql 9.6.5)发送块的二进制数据,但我收到错误的响应(它找我像一些比特缺少)。这是我在Kotlin的代码:
fun getFile(user: User, messageId: Long, fileId: Short, response: HttpServerResponse) {
val mediaContent = mediaContentRepository.getFile(messageId, fileId)
when {
mediaContent == null ->
response.apply { statusCode = 404 }.end()
conversationUserRepository.authorize(user.id!!, mediaContent.message!!.conversation!!.id, mediaContent.message!!.created) ->
response.setChunked(true)
.putHeader("Content-Disposition", "attachment; filename=\"${mediaContent.name}\"") //TODO: prevent injections
.putHeader("Content-Type", mediaContent.contentType)
//.putHeader("Content-Length", mediaContent.file.length().toString())
.apply {
//write(Buffer.buffer().apply { delegate.setBytes(0, mediaContent.file.binaryStream.readAllBytes()) })
val buffer = Buffer.buffer(BUFFER_SIZE)
val stream = mediaContent.file.binaryStream
val byteArray = ByteArray(BUFFER_SIZE)
for (i in (0L..(mediaContent.file.length() / BUFFER_SIZE))) {
val bytesRead = stream.read(byteArray)
buffer.delegate.setBytes(0, byteArray, 0, bytesRead)
write(if (bytesRead == BUFFER_SIZE) buffer else buffer.slice(0, bytesRead))
}
}
.end()
else ->
response.apply { statusCode = 403 }.end()
}
}
但是当我尝试使用此行一次发送整个文件时,响应是正确的。
write(Buffer.buffer().apply { delegate.setBytes(0, mediaContent.file.binaryStream.readAllBytes()) })
知道我做错了吗?
编辑:我通常会收到同一文件的不同回复,这也很有帮助。
答案 0 :(得分:0)
我得到了它的工作,但我不知道为什么它正在工作。它对我来说看起来像循环中重用vert.x缓冲区有问题。代码
fun getFile(user: User, messageId: Long, fileId: Short, response: HttpServerResponse) {
val mediaContent = mediaContentRepository.getFile(messageId, fileId)
when {
mediaContent == null ->
response.apply { statusCode = 404 }.end()
conversationUserRepository.authorize(user.id!!, mediaContent.message!!.conversation!!.id, mediaContent.message!!.created) ->
response.putHeader("Content-Disposition", "attachment; filename=\"${mediaContent.name}\"") //TODO: prevent injections
.putHeader("Content-Type", mediaContent.contentType)
.putHeader("Content-Length", mediaContent.file.length().toString())
.apply {
val stream = mediaContent.file.binaryStream
val byteArray = ByteArray(BUFFER_SIZE)
for (i in (0L..(mediaContent.file.length() / BUFFER_SIZE))) {
val bytesRead = stream.read(byteArray)
write(Buffer(io.vertx.core.buffer.Buffer.buffer(byteArray)
.let {
if (bytesRead == BUFFER_SIZE) it
else it.slice(0, bytesRead)
}))
}
}
.end()
else ->
response.apply { statusCode = 403 }.end()
}
}