0

我正在创建一个允许管理员用户将图像上传到 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
4

1 回答 1

1

这是由 Play 和 H2 之间的交互问题引起的。我感谢这篇文章的部分答案:

http://www.lunatech-research.fr/playframework-file-upload-blob

看来玩!实际上并没有将Blob对象作为二进制大对象放入数据库,而是将其作为文件存储在名为“附件”的文件夹中(其路径是可配置的)。此文件夹中的文件都具有 UUID 作为文件名,事实上,我查看了一个渲染为.png我试图通过我的服务上传的文件。玩什么!然后存储在数据库中的是文件名和内容类型,例如

c73e69c8-3443-489a-b5f0-ef40af99673f|image/jpeg

但是,这里c73e69c8-3443-489a-b5f0-ef40af99673f不是十六进制数字,而是文件名。H2 可能会假设它一个十六进制数并尝试对其进行转换,或者播放!可能正在调用 H2 以便它期望一个十六进制数存储为String. 不管怎样,玩!实际上并没有将 a 存储Blob为 SQL BLOB,String而是将它尝试存储的值解释为十六进制值。

Caused by: org.h2.jdbc.JdbcBatchUpdateException: Hexadecimal string with odd number of characters: "c73e69c8-3443-489a-b5f0-ef40af99673f|image/jpeg";

切换播放!数据库到 MySQL 数据库(无论如何都计划在测试后)使它工作。我不确定这是否可以在仍然使用 H2而不实际重写 H2 或 Play的情况下修复,但如果有人知道修复,我会将其标记为答案。

于 2013-01-07T15:57:31.517 回答