0

I am trying to create a file inside a directory but when the code executes it brings back the error 'java.io.IOException: Invalid file path'. And the code does create the directory called 'ServerUploads' but it does not create the file. Below is a code snippet :

   public static String performUploadOperation(byte[] file, String filename)
        throws IOException {
    //creating a directory to store file.
    //creating a directory to store users
    File userDirectory = new File("C:\\ServerUploads");
    //check if directory does not exist.
    if (!userDirectory.exists()) {
        userDirectory.mkdir();
    }


        File crreate = new File(userDirectory + "\\" +  filename);

        if(!crreate.exists())
        crreate.createNewFile();




    try{
    //convert the bytearray retrieved to a file and upload to server folder.
   FileOutputStream fos = new FileOutputStream(crreate);
   System.out.println(fos.toString());
        //write file to directory.
        fos.write(file);
        fos.close();
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
    sucess = "600 - The file has been successfully uploaded!";
    return sucess;
}

The filename being passed as argument is 'upload.txt' . I am not sure why it is not working. Any help is appreciated. Thanks!. Please see I need the method to return a String not a void as I have to further return it to the client.

4
  • Hi, can you try switching \` to /` in your file constructor? also, what is the byte array for? Commented Mar 16, 2019 at 14:03
  • I have updated my code with the full method and the forward slash did not work unfortunately. Commented Mar 16, 2019 at 14:19
  • @GavinEverett Your code is working apart from return sucess; where it should be either String sucess =.... or directly return "600 - The file has been successfully uploaded!" .Can you add your complete exception from console and how you're calling performUploadOperation(..,...) Commented Mar 16, 2019 at 14:44
  • The createNewFile() call here is a waste of time and space, as are both exists() calls. Commented Mar 16, 2019 at 22:32

2 Answers 2

2

I have found the solution to the problem. The solution was to add '.trim()' to the filename string. There must have been some white-space when the file came in.

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to SO ! Good, as I said your code was working only part we would like to see was how you're calling performUploadOperation(.. , ..) and thus eventually what you're passing in file and filename ! So lesson learnt right? Next time lets add more context/relevance over SO in question and save everyone's time. : )
-1

I tried following code block by just changing return type String to void as nothing was returned. It worked. The folder & file both get created. Here is the code block for same:

public static void performUploadOperation(byte[] file, String filename)
        throws IOException {
    //creating a directory to store file.
    //creating a directory to store users
    File userDirectory = new File("C:\\ServerUploads");
    //check if directory does not exist.
    if (!userDirectory.exists()) {
        userDirectory.mkdir();
    }
    File crreate = new File(userDirectory + "\\" + filename);

    if (!crreate.exists())
        crreate.createNewFile();
}

Calling above function from main:

public static void main(String args[]) throws IOException {
    performUploadOperation("abc".getBytes(),"testfile");
}

3 Comments

you missed to give variable type. It should be String sucess = not just sucess =. Also the content of file will not get printed in the way you wrote your System.out.println(fos.toString()); It should be replaced with : ByteArrayOutputStream stream = new ByteArrayOutputStream(); stream.write(file); System.out.println(new String(stream.toByteArray()));
There is nothing here that solves or even identifies the problem.
The issue was java.io.IOException: Invalid file path given by code posted & the answer I posted solves this problem. When I ran this program on my m/c it didn't gave any error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.