我正在使用Hibernate
。我已将我的列nIcon VARBINARY(MAX)
映射到我的对象中,如下所示
@Column(columnDefinition="binary")
public byte[] getNicon() {
return this.nicon;
}
方法getByteArrayFromFile()用于从图像文件中获取字节,我成功写入数据库。
public static byte[] getByteArrayFromFile(String absoluteFilePath) {
byte [] byteArray = null;
File file = new File(absoluteFilePath);
byteArray = new byte[(int) file.length()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(byteArray);
} catch (FileNotFoundException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
} catch (IOException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
}finally {
if(fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
}
}
}
return byteArray;
}
方法getFileFromByteArray()用于从我从数据库中检索的字节数组中写入图像文件。
public static boolean getFileFromByteArray(String fileName, byte [] byteArray) {
boolean isSuccessful = false;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(byteArray);
isSuccessful = true;
} catch (FileNotFoundException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
} catch (IOException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
} finally {
if (fileOutputStream !=null) {
try {
fileOutputStream.close();
} catch (IOException e) {
logger.error(getStackTrace(e));
e.printStackTrace();
}
}
}
return isSuccessful;
}
我的问题是我使用二进制文件来存储文件。我想在IMAGE
中使用MS SQL Server
类型。
我知道Hibernate
映射@LOB
可用于将图像存储为IMAGE类型。
但是在将字节写回文件时我遇到了问题。