Play Framework 从字节数组下载图像
Play Framework download image from byte array
我有一些用户上传的图像,我将它们存储为 heroku Postgres 数据库中的 byte[ ]。我正在尝试通过类似于以下内容的 http 响应将图像下载为 jpeg。
ImageModel image = ImageModel.getImage(imageID);
response().setContentType("image/jpeg");
response().setHeader("Content-disposition","attachment; filename=image.jpeg");
return ok(image.imageByteArray);
当我尝试 return 它时,我得到一个空指针异常。我正在使用 Ebean 来保存和加载我的 ImageModel。当我将模型放入 JsonNode 和 return 时,我可以看到该字节 [] 的数据。
"id":1,"imageByteArray":"/9j/4AAQSkZJRgABAgAAAQABAAD//gAEKgD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApkZXNjAAAA/AAAAF5jcHJ0AAABXAAAAAt3dHB0AAABaAAAABRia3B0AAABfAAAABRyWFlaAAABkAAAABRnWFlaAAABpAAAABRiWFlaAAABuAAAABRyVFJDAAABzAAAAEBnVFJDAAABzAAAAEBiVFJDAAABzAAAAEBkZXNjAAAAAAAAAANjMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB0ZXh0AAAAAEZCAABYWVogAAAAAAAA9tYAAQAAAADTLVhZWiAAAAAAAAADFgAAAzMAAAKkWFlaIAAAAAAAAG+iAAA49QAAA5BYWVogAAAAAAAAYpkAALeFAAAY2lhZWiAAAAAAAAAkoAAAD4QAALbPY3VydgAAAAAAAAAaAAAAywHJA2MFkghrC/YQPxVRGzQh8SmQMhg7kkYFUXdd7WtwegWJsZp8rGm/fdPD6TD////bAEMACQYHCAcGCQgICAoKCQs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t8PZFmBd4/1D3rb1kyPnaW7sGGpdPr7v6gW9VWUyPxiDsps6q19iU263W8GGBBjn0u/QL5vhLlvS5UB0hO1W/ggcurf0a/3OzFObAMq/qX1FNh0ZZuNR57xtP+CCbFmTjmCAFR0qH99Fq5u+n3iBYDBuuxE4t7n3jMMqnP1iOyrB9MH7il406LlwYZTVKQqtPSN83wl9Llzt2i+xHOufMTQq+E/kvo1gGgq+InIbP3BRk3ppEVcLr7R1RguPqwqCtEHsyksDyZKckctVtucxldDjp9KSDBpo2eXX2mwFHHfWUYmEXBlw6Leegz03ndkfZI/wBp/wAj/bnb/d/yf9p/kERcMN7wYhgK2QVpzV7KfrrfQzKmgL/iIrWu81ka0jilTYngv9wWcstBhFtckRads5NK0IgtsXT3IgbyKu2V/wAgIVBWiJ+X9Q35f+G6FKf0p/wsP52cH2UKv0YrCmu3uP3N3r7D+euJRHVeLEfZ6XEAiCLaMtuLusD/AGK123M0B++ILltC8aVM9KZdhxKqNpVN/MXjaeWvS/ILXyJHSy0Xe7s9pcUo0a/lxcdj7Uwxx6tuXPKXF8PD6PZfz6b+R4/fW3mGTxGiTWn5gLUHzLxAIqg73KS0umNpllaVw1oVy9+SDcA5IY0l58SwpSyJ/gI/s0x1sqUlnBKSkpKR/Y/hm/X2L89bhA1NtIfEOh0fycy5rDABgMBGKNv2D+J331g2Crew/wCMsdBq8J5lnBFs9g7csAAeHpv1fA7M36+wfnpUelvMWXI1+fRt8n2z6Fi381JeJdNmukQjkTRNoTYd8QtY7eeBwEzvL63Lly5fS4vn4Zv0/9k=","processedImage":null}
但是,如果我尝试直接从模型中引用 byte[],则会出现空指针异常。我有点玩游戏,所以我不确定发生了什么。任何帮助将不胜感激。
[error] - application - Exception
java.lang.NullPointerException: null
at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106) ~[na:1.8.0_51]
at controllers.Application.requestImageFile(Application.java:95) ~[classes/:2.4.2]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:208) [classes/:na]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:208) [classes/:na]
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:136) [play_2.11-2.4.2.jar:2.4.2]
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$$anon$$anon.invocation(HandlerInvoker.scala:127) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anon.call(JavaAction.scala:70) [play_2.11-2.4.2.jar:2.4.2]
at play.http.DefaultHttpRequestHandler.call(DefaultHttpRequestHandler.java:20) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:40) [play_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) [play_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$.apply(Future.scala:31) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$.apply(Future.scala:492) [scala-library-2.11.6.jar:na]
at play.core.j.JavaAction.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.2.jar:2.4.2]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.2.jar:2.4.2]
at scala.Option.map(Option.scala:146) [scala-library-2.11.6.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) [play_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.11.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]
我个人需要更多信息来调试它,但是,也许您可以根据我如何使用 Ebean 在 PostgreSQL 中存储和检索图像的示例获得继续所需的信息。
模型看起来像这样:
public class User extends Model {
public static Finder<LInteger, User> find = new Finder<Integer,User>(User.class);
@Id
public Integer id;
public String name;
@Constraints.Required
@Constraints.Email
public String email;
public String password;
@Lob
public byte[] photo;
public static User findByEmail(String email) {
return find.where().eq("email", email).findUnique();
}
}
控制器看起来像这样:
public class Application extends Controller {
public static Result userPhoto(Integer userId) {
User user = User.find.ById(userId);
if (user == null) {
return notFound();
}
if (user.photo == null) {
return ok(Play.application().getFile("/public/images/default-user.jpg"), true).as("image/jpeg");
}
return ok(new ByteArrayInputStream(user.photo)).as("image/jpeg");
}
}
希望对您有所帮助!
我要做的第一件事是在您获得 ImageModel 后添加一个 null 检查。例如:
ImageModel image = ImageModel.getImage(imageID);
if (image == null){
Logger.error("image not retrieved ...");
}
如果它不为空,那么您是否使用 EBean 在数据库中存储内容?您是否将 imageByteArray 注释为 @Lob?尝试进入您的数据库并确保该列有一个值并且它不为空,如果稍后发现它为空,则首先保存 lob 可能会出现问题。
另外,如果你想下载图片,你的内容类型应该是
response().setContentType("application/x-download");
看来您需要将字节数组包装到 ByteArrayInputStream
ByteArrayInputStream input = new ByteArrayInputStream(image.imageByteArray);
return ok(input);
请查看此问题的答案:
Displaying image object from controller in the browser
我有一些用户上传的图像,我将它们存储为 heroku Postgres 数据库中的 byte[ ]。我正在尝试通过类似于以下内容的 http 响应将图像下载为 jpeg。
ImageModel image = ImageModel.getImage(imageID);
response().setContentType("image/jpeg");
response().setHeader("Content-disposition","attachment; filename=image.jpeg");
return ok(image.imageByteArray);
当我尝试 return 它时,我得到一个空指针异常。我正在使用 Ebean 来保存和加载我的 ImageModel。当我将模型放入 JsonNode 和 return 时,我可以看到该字节 [] 的数据。
"id":1,"imageByteArray":"/9j/4AAQSkZJRgABAgAAAQABAAD//gAEKgD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApkZXNjAAAA/AAAAF5jcHJ0AAABXAAAAAt3dHB0AAABaAAAABRia3B0AAABfAAAABRyWFlaAAABkAAAABRnWFlaAAABpAAAABRiWFlaAAABuAAAABRyVFJDAAABzAAAAEBnVFJDAAABzAAAAEBiVFJDAAABzAAAAEBkZXNjAAAAAAAAAANjMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB0ZXh0AAAAAEZCAABYWVogAAAAAAAA9tYAAQAAAADTLVhZWiAAAAAAAAADFgAAAzMAAAKkWFlaIAAAAAAAAG+iAAA49QAAA5BYWVogAAAAAAAAYpkAALeFAAAY2lhZWiAAAAAAAAAkoAAAD4QAALbPY3VydgAAAAAAAAAaAAAAywHJA2MFkghrC/YQPxVRGzQh8SmQMhg7kkYFUXdd7WtwegWJsZp8rGm/fdPD6TD////bAEMACQYHCAcGCQgICAoKCQsOFw8ODQ0OHBQVERciHiMjIR4gICUqNS0lJzIoICAuPy8yNzk8PDwkLUJGQTpGNTs8Of/bAEMBCgoKDgwOGw8PGzkmIC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a/3OzFObAMq/qX1FNh0ZZuNR57xtP+CCbFmTjmCAFR0qH99Fq5u+n3iBYDBuuxE4t7n3jMMqnP1iOyrB9MH7il406LlwYZTVKQqtPSN83wl9Llzt2i+xHOufMTQq+E/kvo1gGgq+InIbP3BRk3ppEVcLr7R1RguPqwqCtEHsyksDyZKckctVtucxldDjp9KSDBpo2eXX2mwFHHfWUYmEXBlw6Leegz03ndkfZI/wBp/wAj/bnb/d/yf9p/kERcMN7wYhgK2QVpzV7KfrrfQzKmgL/iIrWu81ka0jilTYngv9wWcstBhFtckRads5NK0IgtsXT3IgbyKu2V/wAgIVBWiJ+X9Q35f+G6FKf0p/wsP52cH2UKv0YrCmu3uP3N3r7D+euJRHVeLEfZ6XEAiCLaMtuLusD/AGK123M0B++ILltC8aVM9KZdhxKqNpVN/MXjaeWvS/ILXyJHSy0Xe7s9pcUo0a/lxcdj7Uwxx6tuXPKXF8PD6PZfz6b+R4/fW3mGTxGiTWn5gLUHzLxAIqg73KS0umNpllaVw1oVy9+SDcA5IY0l58SwpSyJ/gI/s0x1sqUlnBKSkpKR/Y/hm/X2L89bhA1NtIfEOh0fycy5rDABgMBGKNv2D+J331g2Crew/wCMsdBq8J5lnBFs9g7csAAeHpv1fA7M36+wfnpUelvMWXI1+fRt8n2z6Fi381JeJdNmukQjkTRNoTYd8QtY7eeBwEzvL63Lly5fS4vn4Zv0/9k=","processedImage":null}
但是,如果我尝试直接从模型中引用 byte[],则会出现空指针异常。我有点玩游戏,所以我不确定发生了什么。任何帮助将不胜感激。
[error] - application - Exception
java.lang.NullPointerException: null
at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106) ~[na:1.8.0_51]
at controllers.Application.requestImageFile(Application.java:95) ~[classes/:2.4.2]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:208) [classes/:na]
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:208) [classes/:na]
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:136) [play_2.11-2.4.2.jar:2.4.2]
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$$anon$$anon.invocation(HandlerInvoker.scala:127) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anon.call(JavaAction.scala:70) [play_2.11-2.4.2.jar:2.4.2]
at play.http.DefaultHttpRequestHandler.call(DefaultHttpRequestHandler.java:20) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:40) [play_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) [play_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$.apply(Future.scala:31) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$.apply(Future.scala:492) [scala-library-2.11.6.jar:na]
at play.core.j.JavaAction.apply(JavaAction.scala:94) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) [play_2.11-2.4.2.jar:2.4.2]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.2.jar:2.4.2]
at scala.Option.map(Option.scala:146) [scala-library-2.11.6.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) [play_2.11-2.4.2.jar:2.4.2]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) [play_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM.apply(Iteratee.scala:524) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM.apply(Iteratee.scala:560) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$$anonfun$apply.apply(Iteratee.scala:536) [play-iteratees_2.11-2.4.2.jar:2.4.2]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.11.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]
我个人需要更多信息来调试它,但是,也许您可以根据我如何使用 Ebean 在 PostgreSQL 中存储和检索图像的示例获得继续所需的信息。
模型看起来像这样:
public class User extends Model {
public static Finder<LInteger, User> find = new Finder<Integer,User>(User.class);
@Id
public Integer id;
public String name;
@Constraints.Required
@Constraints.Email
public String email;
public String password;
@Lob
public byte[] photo;
public static User findByEmail(String email) {
return find.where().eq("email", email).findUnique();
}
}
控制器看起来像这样:
public class Application extends Controller {
public static Result userPhoto(Integer userId) {
User user = User.find.ById(userId);
if (user == null) {
return notFound();
}
if (user.photo == null) {
return ok(Play.application().getFile("/public/images/default-user.jpg"), true).as("image/jpeg");
}
return ok(new ByteArrayInputStream(user.photo)).as("image/jpeg");
}
}
希望对您有所帮助!
我要做的第一件事是在您获得 ImageModel 后添加一个 null 检查。例如:
ImageModel image = ImageModel.getImage(imageID);
if (image == null){
Logger.error("image not retrieved ...");
}
如果它不为空,那么您是否使用 EBean 在数据库中存储内容?您是否将 imageByteArray 注释为 @Lob?尝试进入您的数据库并确保该列有一个值并且它不为空,如果稍后发现它为空,则首先保存 lob 可能会出现问题。
另外,如果你想下载图片,你的内容类型应该是
response().setContentType("application/x-download");
看来您需要将字节数组包装到 ByteArrayInputStream
ByteArrayInputStream input = new ByteArrayInputStream(image.imageByteArray);
return ok(input);
请查看此问题的答案: Displaying image object from controller in the browser