简体   繁体   English

使用Anorm(Scala / PlayFramework 2)在Postgres中保存/加载图像

[英]Saving / Loading Images in Postgres using Anorm (Scala/PlayFramework 2)

I think I'm saving the image to Postgres correctly, but get unexpected results trying to load the image. 我认为我已将图像正确保存到Postgres,但是尝试加载图像时会得到意外的结果。 I don't really know if the error is in save or load. 我真的不知道错误是否在保存或加载中。

Here is my Anorm code for saving the image: 这是我保存图像的Anorm代码:

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()
    }
  }
}

} }

...I get "update succesful", so I presume the save is working (I could be wrong). ...我得到“更新成功”,因此我认为保存正在工作(我可能错了)。 Here is my code for loading the image: 这是我用于加载图像的代码:

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"
      }
  }
}

} }

...rather than going into the case "Row(image: Array[Byte])" as hoped, it goes into the "Row(Some(unknown: Any))" case. ...而不是像希望的那样进入“ Row(image:Array [Byte])”情况,而是进入“ Row(Some(unknown:Any))”情况。 My println outputs "[B@11be1c6 unknown type is [B" 我的println输出“ [B @ 11be1c6未知类型为[B”

I don't know what type [B is or where I may have gone wrong... 我不知道[B是什么类型,或者我可能在哪里出错了...

It's an array of byte in Java(byte[]). 它是Java(byte [])中的字节数组。 > "I don't know what type [B". >“我不知道[B]是什么类型。

And You can write match { case Row(Some(image: Array[Byte])) => } too in this case and that might be better. 并且在这种情况下,您也可以编写match { case Row(Some(image: Array[Byte])) => } ,这样可能会更好。

Or you might be able to do that as follows. 或者,您可以按照以下说明进行操作。

val results: Stream[Array[Byte]] = SQL(cmd stripMargin)
  .on("badgeHandle" -> "name")().map { row => row[Array[Byte]]("img") }

...Oops, got the following compile error. ...糟糕,出现以下编译错误。

<console>:43: error: could not find implicit value for parameter c: anorm.Column[Array[Byte]]
          val res: Stream[Array[Byte]] = SQL(cmd stripMargin).on("badgeHandle" -> "name")().map { row => row[Array[Byte]]("img") }

Unfortunately, scala.Array is not supported by default. 不幸的是,默认情况下不支持scala.Array If you imitate the way of other types, It works. 如果您模仿其他类型的方式,它会起作用。

implicit def rowToByteArray: Column[Array[Byte]] = {
  Column.nonNull[Array[Byte]] { (value, meta) =>
    val MetaDataItem(qualified, nullable, clazz) = meta
    value match {
      case bytes: Array[Byte] => Right(bytes)
      case _ => Left(TypeDoesNotMatch("..."))
    }
  }
}
val results: Stream[Array[Byte]] = SQL(cmd stripMargin)
  .on("badgeHandle" -> "name")().map { row => row[Array[Byte]]("img") }

https://github.com/playframework/Play20/blob/master/framework/src/anorm/src/main/scala/anorm/Anorm.scala https://github.com/playframework/Play20/blob/master/framework/src/anorm/src/main/scala/anorm/Anorm.scala

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM