Question

Hi i am trying to upload a file using spring data. When i try to upload file, i get an exception.

My code for file upload is

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();
    }

where TEMPORARY_FILES_DIRECTORY = new File("/home/temp/");

the exception i am getting is

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

on debugging FileUtils class

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

parent.mkdirs() is false. 

Can anyone kindly tell me what is wrong with this code.

Was it helpful?

Solution

Are you sure it's /home/temp and not /home/username/temp? You can't create directories outside your home directory. Try something like Systen.getProperty("user.home") + "/temp", if you'd like to store the files inside your home directory. Anyway, why didn't you choose /tmp to be your temporary directory?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top