2013-03-22 88 views
1

實體:插入圖像中的數據庫與BLOB數據類型

@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 

但如果我只上傳10kb的圖片,代碼就起作用。

使用JSF 2.0,Primefaces 3.5,大多數代碼是使用CRUD自動生成的。

+0

你如何聲明你的數據庫列? – skuntsel 2013-03-22 06:12:05

+0

這是一個JPA問題,而不是JSF。 – 2013-03-22 06:14:00

+0

我宣佈它爲BLOB – galao 2013-03-22 06:49:30

回答

1

問題是您的數據庫列設置爲存儲的內容比您嘗試存儲的內容少。這就是截斷手段。

您需要將您的數據庫列定義更改爲LONGBLOB。

+0

,但我只是上傳一個84kb大小的圖像 – galao 2013-03-22 06:50:24

+0

您可能想要[MySQL存儲要求](http://dev.mysql.com/doc/refman/5.5/en/storage-requirements.html #id899830)。 – Ares 2013-03-22 06:54:37