簡體   English   中英

如何將文件(圖片,pdf)保存到ibm db2中

[英]How to save a file(picture, pdf) into ibm db2

我正在嘗試獲取代碼以加載圖片並將其保存在DB2中。 我不確定下一步該怎么做,因為下面的代碼失敗了。 有人可以指導我如何執行此操作。 我已經從數據庫生成了一個實體。 documentname(實際文件)的數據類型是blob,但是當我生成實體時,它顯示為byte []。 下面的方法在EJB中。

@Entity
@Table(name = "DOCUMENTS", schema = "PORTAL")
public class Documents { 
private int documentid;
private byte[] documentname;

@Id
@Column(name = "DOCUMENTID", nullable = false)
public int getDocumentid() {
    return documentid;
}
public void setDocumentid(int documentid) {
    this.documentid = documentid;
}
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "DOCUMENTNAME", nullable = true)
public byte[] getDocumentname() {
    return documentname;
}

public void setDocumentname(byte[] documentname) {
    this.documentname = documentname;
}

在這里,我正在嘗試閱讀或加載圖片。

    private byte[] readImage(String filename) {

    byte[]  imageData = null;
    FileInputStream file = null;
    try {
        file = new FileInputStream(filename);
        int size = file.available();
        imageData = new byte[size];
        file.read(imageData);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (file != null) file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return imageData;
}

我認為這基本上就是我要放的地方。

public Boolean populateProfilePicture(int blobFileID, byte[] blobFIle) {

    Connection con = null;
    PreparedStatement pstmt = null;
    int success = 0;

    String empPhotoFile = "/home/mika/Pictures";
    Documents fileTable = new Documents();
    try {
        con = dataSource.getConnection();
        pstmt = con.prepareStatement("INSERT INTO Documents VALUES(?,?)");
        pstmt.setInt(1, blobFileID);
        pstmt.setBytes(2, readImage(empPhotoFile));
        success = pstmt.executeUpdate();
    } catch (SQLException e) {
        try {
            con.close();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }

    if (success>=1)
    {
        return true;
    }

    return true;
}

byte []是適合BLOB數據的類型映射,盡管您可能想研究一下JPA實現-它可能包含有關如何影響該映射成為InputStream等的詳細信息,因此您可以使用流式API,具體取決於手頭文件的大小。

就是說,由於您是將原始數據讀取到字節數組中,因此,考慮到實現readData()的方式,似乎您實際上並不在乎。

由於您使用的是JPA,因此可能會建議您使用JPA。

public byte[] readImage(Entity entity String filename) throws Exception {

    byte[]  imageData = null;
    try ( FileInputStream file : file = new FileInputStream(filename) ){

        int size = file.available();
        imageData = new byte[size];
        file.read(imageData);
        return imageData;
    } catch (IOException | FileNotFoundException e) {
        throw e;
    } 
}

這只是做同樣的事情,但是它將數據放在JPA實體中,而不是試圖弄亂連接。 當您的實體經理提交工作單元時,應該序列化到數據庫正常。

編輯:這是根據要求重寫您的populate *方法。 請注意,我正在手動管理交易,並對您的entitymanager做出了許多假設。 那東西是一個更大/不同的問題。

public void populateProfilePicture(int blobFileID, String employeePhoto) throws Exception {

    // this is about you figuring out JPA.
    EntityManager entityManager = getEntityManager()
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin()
    try {

    Documents documents = entityManager.find(Documents.class, blobFileID);
    byte[] data readImage( employeePhoto );
    documents.setDocumentname( data );
    } catch(Exception e) {
        transaction.setRollbackOnly();
        throw e
    }
    } finally {
        transaction.commit();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM