如何将文件(图片,pdf)保存到ibm db2中

时间:2018-03-06 07:17:19

标签: java jpa db2 ejb prepared-statement

我试图让我的代码加载图片并将其保存在DB2中。我不知道下一步该做什么,因为下面的代码失败了。有人可以指导我如何做到这一点。我从DB生成了一个实体。 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;
}

1 个答案:

答案 0 :(得分:1)

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 *方法。请注意,我手动管理事务,并对您的实体管理器做出很多假设。那些东西是一个更大/不同的问题。

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();
    }
}
相关问题