我正在创建一个允许管理员用户将图像上传到 Play 的 POST 服务!应用程序,当我测试它时,我正在使用内置的 H2 文件系统数据库。根据
https://gist.github.com/1256286#file-picture-java-L3
在 Play 中存储这些图像的好方法!是一个字节数组,然后使用Controller#renderBinary(...)
.
此函数中的Campaign
对象存储有关活动的所有数据并具有Banner
对象列表,每个对象基本上是一个图像文件和一些元数据。自定义下的以下方法Controller
应该接收要上传的图像:
public static void putCampaignBanner (Upload bannerFile) {
Campaign campaign = findCampaignByIdOrNew();
if(campaign.banners == null) {
campaign.banners = new LinkedList<Banner>();
}
String name = params.get("name", String.class);
int width = 0;
int height = 0;
try {
BufferedImage image = ImageIO.read(bannerFile.asFile());
width = image.getWidth();
height = image.getHeight();
} catch (IOException e) {
//it's not a picture. die gracefully.
e.printStackTrace();
renderJSON(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(campaign));
return;
}
if(name==null||name.length()==0) {
//no name. die gracefully. like a swan.
renderJSON(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(campaign));
return;
}
Banner banner=null;
for(Banner b: campaign.banners) {
if(b.name.equals(name)) {
banner = b;
break;
}
}
if(banner==null) {
banner=new Banner();
banner.name=name;
banner.campaign = campaign;
banner.save();
campaign.banners.add(banner);
}
//validate file and populate banner
//__ vv THIS LINE CAUSES THE EXCEPTION__
banner.file = bannerFile.asBytes();
//__ ^^ REMOVING THIS LINE PREVENTS THE EXCEPTION__
banner.contenttype = bannerFile.getContentType();
banner.width = width;
banner.height = height;
banner.url = getRouterUrlWithId("ImageService.getById", banner.id);
//__ vv THIS LINE THROWS THE EXCEPTION__
campaign.save();
banner.save();
renderJSON(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(campaign));
}
我正在等待另一个开发人员完成上传表单,所以我正在使用 curl 进行测试:
curl http://[ourserver]:9000/TemplatePopulationController/putCampaignBanner -i -F campaignPrimaryId=44 -F name=headerBanner -F bannerFile=@tempimages/pissybiscuit.png
显然我需要存储图像。如何防止 PersistenceException?
更新我尝试将@Lob 注释添加到,byte[]
并将参数类型更改为Blob
并因此持久化对象。两者都没有奏效。
检查日志显示服务器遇到了 GenericJDBCException:
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Hexadecimal string with odd number of characters: "c73e69c8-3443-489a-b5f0-ef40af99673f|image/jpeg";
错误实例的日志是:
@6d0npdn4l
Internal Server Error (500) for request POST /TemplatePopulationController/putCampaignBanner
Execution exception (In /app/controllers/TemplatePopulationController.java around line 285)
PersistenceException occured : update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
play.exceptions.JavaExecutionException: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:229)
at Invocation.HTTP Request(Play!)
Caused by: javax.persistence.PersistenceException: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
at play.db.jpa.JPABase._save(JPABase.java:50)
at play.db.jpa.GenericModel.save(GenericModel.java:184)
at controllers.TemplatePopulationController.putCampaignBanner(TemplatePopulationController.java:285)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
... 1 more
16:06:36,553 INFO ~ campaignPrimaryId=8
16:06:36,571 WARN ~ SQL Error: 90003, SQLState: 90003
16:06:36,571 ERROR ~ Hexadecimal string with odd number of characters: "363438fc-f232-401e-9f69-7db499a24ba4|application/octet-stream"; SQL statement: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=? [90003-170]
16:06:36,572 WARN ~ SQL Error: 90003, SQLState: 90003
16:06:36,572 ERROR ~ Hexadecimal string with odd number of characters: "363438fc-f232-401e-9f69-7db499a24ba4|application/octet-stream"; SQL statement: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=? [90003-170]
16:06:36,572 ERROR ~ Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
at play.db.jpa.JPABase._save(JPABase.java:47)
at play.db.jpa.GenericModel.save(GenericModel.java:184)
at controllers.TemplatePopulationController.putCampaignBanner(TemplatePopulationController.java:285)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:220)
at play.Invoker$Invocation.run(Invoker.java:265)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:200)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Hexadecimal string with odd number of characters: "363438fc-f232-401e-9f69-7db499a24ba4|application/octet-stream"; SQL statement: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=? [90003-170]
at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1121)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 29 more
16:06:36,591 ERROR ~
@6d0npdn4m
Internal Server Error (500) for request POST /TemplatePopulationController/putCampaignBanner
Execution exception (In /app/controllers/TemplatePopulationController.java around line 285)
PersistenceException occured : update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
play.exceptions.JavaExecutionException: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:229)
at Invocation.HTTP Request(Play!)
Caused by: javax.persistence.PersistenceException: update Banner set campaign_id=?, file=?, height=?, name=?, url=?, width=? where id=?
at play.db.jpa.JPABase._save(JPABase.java:50)
at play.db.jpa.GenericModel.save(GenericModel.java:184)
at controllers.TemplatePopulationController.putCampaignBanner(TemplatePopulationController.java:285)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
... 1 more