我想我正在正确地将图像保存到 Postgres,但是在尝试加载图像时会得到意想不到的结果。我真的不知道错误是在保存还是加载中。
这是我保存图像的异常代码:
def storeBadgeImage(badgeHandle: String, imgFile: File) = {
val cmd = """
|update badge
|set img={imgBytes}
|where handle = {badgeHandle}
"""
var fis = new FileInputStream(imgFile)
var imgBytes: Array[Byte] = Resource.fromInputStream(fis).byteArray
// at this point I see the image in my browser if I return the imgBytes in the HTTP response, so I'm good so far.
DB.withConnection { implicit c =>
{
try {
SQL(cmd stripMargin).on("badgeHandle" -> badgeHandle, "imgBytes" -> imgBytes).executeUpdate() match {
case 0 => "update failed for badge " + badgeHandle + ", image " + imgFile.getCanonicalPath
case _ => "Update Successful"
}
} catch {
case e: SQLException => e.toString()
}
}
}
}
...我得到“更新成功”,所以我认为保存正在工作(我可能是错的)。这是我加载图像的代码:
def fetchBadgeImage(badgeHandle: String) = {
val cmd = """
|select img from badge
|where handle = {badgeHandle}
"""
DB.withConnection { implicit c =>
SQL(cmd stripMargin).on("badgeHandle" -> badgeHandle)().map {
case Row(image: Array[Byte]) => {
"image = " + image
}
case Row(Some(unknown: Any)) => {
println(unknown + " unknown type is " + unknown.getClass.getName) //[B@11be1c6 unknown type is [B
"unknown"
}
}
}
}
...而不是像希望的那样进入“Row(image:Array [Byte])”案例,而是进入“Row(Some(unknown:Any))”案例。我的 println 输出“[B@11be1c6 未知类型是 [B”
我不知道 [B 是什么类型,或者我可能哪里出错了......