1 回答
TA贡献1811条经验 获得超5个赞
最常见的文件存储方式是byte[], Clob,Blob。您已经创建了一个 FileOutputStream 来写入文件内容,但您没有用数据填充它。因此,您可能在以下两种情况之一中失败了:
你只保留数据库中的文件名,如果是这样,那么文件从一开始就是空的
您使用了路径中的内容,创建了输出文件的无效路径,首先记录路径,它是否正确。
所以让我们假设你有这个模型:
class ProjectsDO {
private String fileName;
private byte[] fileContent;
// getters and setters
}
你的方法看起来像这样:
try{
session = getSession();
ProjectsDO project = em.find(ProjectsDO.class, id);
// read the file name or the full path
String fileName=project.getFileName();
// read the content of the file
byte[] fileContent = project.getFileContent();
//compute the output file path (combine directory and file name)
Path path = Paths.get("C:/Users/veerraju/Desktop/sap/projects/"+fileName);
// this path was missing - fill the file with content
Files.write(path,fileContent);
System.out.println("file created succefully");
} catch(IOException e){
System.out.println(e.getMessage());
}
当然,您可以对文件内容进行编码,但为了简化此任务,我跳过了这一部分。
更新:
为了将文件转换为 bytes[]:
byte[] fileContent = Files.readAllBytes(file.toPath());
如果您的数据库不支持 byte[] 列类型,那么您将需要将其持久化为 varchar/text 列,但性能会更差。
添加回答
举报