0

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.

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79

1 Answers1

2

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?

tkroman
  • 4,811
  • 1
  • 26
  • 46
  • thanks for response, i changed it to /home/username/temp, and it worked [answer accepted] - suggestion for using '/tmp', +1 – Shahzeb Khan Feb 19 '14 at 11:17