有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用spring数据gridfs上传java文件

嗨,我正在尝试使用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/");

我得到的例外是

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. 

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


共 (1) 个答案

  1. # 1 楼答案

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