从mysql blob中读取java.io.File

时间:2018-05-09 09:20:10

标签: java mysql blob

我有以下代码,它存储List to File,然后存储到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)

1 个答案:

答案 0 :(得分:0)

正如@EJP所提到的,java.io.File只不过是文件名的持有者......理解错误 - 所以文件的内容都没有......