从mysql blob读取java.io.file

ma8fv8wu  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(358)

我有下面的代码,它将一个列表存储到文件,然后存储到mysql blob列。是否可以从数据库中恢复该列表?

ArrayList<String> myList = new ArrayList<String>();

File tempFile = File.createTempFile("myFile.temp");

tempFile.deleteOnExit();

FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeUnshared(myList);
objectOutputStream.close();

String trailQuery = "INSERT INTO table (tempFile) VALUES (?)";
PreparedStatement preparedStatement = connection.prepareStatement(trailQuery);
preparedStatement.setObject(1, tempFile);

我试着读这个,但我得到一个filenotfoundexception

if (resultSet.next()) {

    InputStream fileInputStream = resultSet.getBinaryStream("tempFile");
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    File inner = (File) objectInputStream.readObject();
    FileInputStream fileInputStreamInner = new FileInputStream(inner);
    ObjectInputStream objectInputStreamInner = new ObjectInputStream(fileInputStreamInner);

    ArrayList<String> myList = (ArrayList<String>) objectInputStreamInner.readObject();
    System.out.println(myList.size());
}

例外

java.io.FileNotFoundException: \tmp\myFile.1708309680603860570.temp (The system cannot find the path specified)
zour9fqk

zour9fqk1#

正如@ejp提到的,java.io.file只不过是一个文件名的持有者。。。有错误的理解-所以文件的内容都不在那里。。。。

相关问题