【问题标题】:How to save a file(picture, pdf) into ibm db2如何将文件(图片,pdf)保存到 ibm db2
【发布时间】:2018-08-13 23:38:24
【问题描述】:

我正在尝试让我的代码加载图片并将其保存在 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;
}

【问题讨论】:

    标签: java jpa db2 ejb prepared-statement


    【解决方案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* 方法。请注意,我正在手动管理事务,并对您的 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();
        }
    }
    

    【讨论】:

    • @Iscoughlin 我有点迷茫,我找到了该方法的更新,但我不知道将它与另一个相关联,因为它没有返回类型。你能帮我把那个方法和其他 populateProfilePicture 一起使用吗?
    • @Mika 为了清晰起见,我对内容进行了一些编辑。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    相关资源
    最近更新 更多