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

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

我试图让我的代码加载图片并将其保存在 DB2 中。我不确定下一步该怎么做,因为下面的代码失败了。有人可以指导我如何做到这一点。我已经从数据库生成了一个实体。文档名(实际文件)的数据类型是 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* 方法的 re-write 请求。请注意,我正在手动管理事务,并对您的实体管理器做出很多假设。那是一个 bigger/different 问题。

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();
    }
}