【问题标题】:inserting an image in the database with a BLOB datatype使用 BLOB 数据类型在数据库中插入图像
【发布时间】:2013-03-22 03:37:04
【问题描述】:

实体:

@Lob
@Column(name = "logo")
private byte[] logo;

表格:

<h:form enctype="multipart/form-data">  
    <p:messages showDetail="true"/>  

    <p:fileUpload value="#{testController.file}" 
                  update="messages"  
                  mode="simple"
                  sizeLimit="1048576"   
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>  

    <p:growl id="messages" showDetail="true"/>  

    <p:commandButton value="Submit" ajax="false"  
                     actionListener="#{testController.upload}"/>  
</h:form>  

豆子:

private testEntity current;
private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void upload() {
    if (file != null) {
        try {
            byte[] data = file.getContents();
            current.setLogo(data);

            getFacade().edit(current);
            JsfUtil.addSuccessMessage("Successful! " + file.getFileName() + " is uploaded.");
        } catch (Exception e) {
        }
    }
}

当我尝试上传像 80kb 图片这样的文件时,它会给我这个异常

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'logo' at row 1

但如果我只上传

使用 JSF 2.0、Primefaces 3.5,大多数代码都是使用 CRUD 自动生成的。

【问题讨论】:

  • 如何声明数据库列?
  • 这是 JPA 问题,不是 JSF。
  • 我将其声明为 BLOB

标签: mysql jpa jpa-2.0


【解决方案1】:

问题是您的数据库列设置为存储的内容少于您尝试存储的内容。这就是截断的意思。

您需要将数据库列定义更改为 LONGBLOB。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2023-03-03
  • 2021-06-01
  • 1970-01-01
  • 2020-02-21
  • 2014-01-31
  • 2012-10-23
相关资源
最近更新 更多