public String addFile(UploadFileBean uploadFileBean) throws Exception {
SessionFactory sessionFactory = HibernateUtility.createSessionFactory();
Session session = null;
session = sessionFactory.openSession();
session.beginTransaction();
UploadFileEntity uploadFileEntity = new UploadFileEntity();
InputStream input = uploadFileBean.getFileData().getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
for (int length = 0; (length = input.read(buffer)) > 0;)
output.write(buffer, 0, length);
uploadFileEntity.setUploaderId(uploadFileBean.getUploaderId());
uploadFileEntity.setReviewerId(uploadFileBean.getReviewerId());
uploadFileEntity.setFileData(output.toByteArray());
uploadFileEntity.setFileName(uploadFileBean.getFileName());
uploadFileEntity.setFileStatus("Pending");
session.persist(uploadFileEntity);
session.getTransaction().commit();
session.close();
return null;
}
这是我用来将部分文件转换为字节并以BLOB格式将其保存到数据库的代码。但是一旦代码完全执行,没有任何东西被保存到数据库中,它显示为null。有人可以建议可能的错误吗?
答案 0 :(得分:2)
您的输出未关闭。数据仍在缓存中,需要刷新。