美好的一天,我试图测试将图像上传到我的数据库,但当我尝试检索图像时,没有显示任何内容..
这是我上传图像的代码
String m_dataForImage;
public String getDataForImage() { return m_dataForImage; }
public void setDataForImage(String value) { this.m_dataForImage = value; }
String m_ShowPhoto;
public String getShowPhoto() { return m_ShowPhoto; }
public void setShowPhoto(String value) { this.m_ShowPhoto = value; }
String m_fileName;
public String getFileName() { return m_fileName; }
public void setFileName(String value) { this.m_fileName = value; }
public void onUploadFile(javax.faces.event.ActionEvent event) {
if (event instanceof BaseActionEventUpload){
try{
ConnectionPool.getInstance();
Base.open(ConnectionPool.dataSourcePooled);
BaseActionEventUpload bae =(BaseActionEventUpload)event;
setFileName(bae.getClientFileName());
m_ShowPhoto = bae.getHexByteString();
byte[] imageByte = bae.getHexBytes();
setShowPhoto("/hex("+ValueManager.encodeHexString(imageByte)+")");//trying to convert byte to hex
photo_storage photo = new photo_storage();
photo.set("timestamp","ss");
photo.set("Primary_Key", "123");
photo.set("File_Name",getFileName());
photo.set("Image_Data",getShowPhoto());
photo.saveIt();
}catch(Throwable t){
t.printStackTrace();
Statusbar.outputAlert(t.toString());
}finally {
Base.close();
}
}
}
并显示图像:
public void onShowImage(javax.faces.event.ActionEvent event) {
try{
ConnectionPool.getInstance();
Base.open(ConnectionPool.dataSourcePooled);
List<photo_storage> photo = photo_storage.where("Primary_Key = ?",123);
for (photo_storage so:photo){ setShowPhoto("/hex("+ValueManager.encodeHexString(so.getBytes("Image_Data"))+")");
setFileName(so.getString("File_Name"));
setDataForImage(so.getString("Image_Data"));
}catch(Throwable t){
t.printStackTrace();
Statusbar.outputAlert(t.toString());
}finally {
Base.close();
}
}
我目前使用 Intellij 和 CaptainCasa..
有人可以帮忙吗? 提前致谢。
最佳答案
看来应该是
photo.set("Image_Data",imageByte);
而不是
photo.set("Image_Data",getShowPhoto());
因为imageByte是上传图片的存储位置。
关于java - 使用 MySQL Workbench 保存和显示数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49688029/