Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Hibernate保存从MS SQL Server检索图像的广告-使用图像类型_Java_Sql Server_Hibernate - Fatal编程技术网

Java 使用Hibernate保存从MS SQL Server检索图像的广告-使用图像类型

Java 使用Hibernate保存从MS SQL Server检索图像的广告-使用图像类型,java,sql-server,hibernate,Java,Sql Server,Hibernate,我正在使用Hibernate。我在我的对象中映射了我的列nIconVARBINARY(MAX),如下所示 @Column(columnDefinition="binary") public byte[] getNicon() { return this.nicon; } 方法getByteArrayFromFile()用于从图像文件中获取字节,我成功地将其写入数据库 public static byte[] getByteArrayFromFile

我正在使用
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;
        }
        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;
        }
方法getFileFromByteArray()用于从我从数据库检索的字节数组中写入图像文件

    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;
        }
        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;
        }
我的问题是我正在使用二进制文件来存储文件。我想使用
图像
键入
MS SQL Server

我知道可以使用
Hibernate
映射
@LOB
将图像存储为图像类型。
但是我在将字节写回文件时遇到问题。

为什么要更改图像数据类型?它已被弃用。您应该改为使用varbinary(max)。你好客户端的数据库已经使用图像数据类型。因此将其更改为varbinary。