gpt4 book ai didi

java - 使用 spring 数据上传文件 - gridfs

转载 作者:行者123 更新时间:2023-12-02 06:11:03 24 4
gpt4 key购买 nike

嗨,我正在尝试使用 spring 数据上传文件。当我尝试上传文件时,出现异常。

我的文件上传代码是

try {
File file = new File(this.TEMPORARY_FILES_DIRECTORY, Calendar.getInstance().getTimeInMillis() + "_" + fileNameUnderscored);
writeByteArrayToFile(file, form.getFile().getBytes());
FileInputStream inputStream = new FileInputStream(file);
GridFSFile gridFSFile = gridFsTemplate.store(inputStream, "test.png");
PropertyImage img = new PropertyImage();
img.setPropertyUid(gridFSFile.getFilename());
imagesRepository.save(img);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

其中 TEMPORARY_FILES_DIRECTORY = new File("/home/temp/");

我得到的异常(exception)是

java.io.IOException: File '/home/temp/1392807425028_file' could not be created

调试FileUtils

if (parent.mkdirs() == false) {
throw new IOException("File '" + file + "' could not be created");
}

parent.mkdirs() is false.

谁能告诉我这段代码有什么问题吗?

最佳答案

您确定是 /home/temp 而不是 /home/username/temp 吗?您无法在主目录之外创建目录。如果您想将文件存储在主目录中,请尝试使用 Systen.getProperty("user.home") + "/temp" 之类的方法。不管怎样,为什么不选择 /tmp 作为临时目录?

关于java - 使用 spring 数据上传文件 - gridfs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com