【问题标题】:java.io.IOException: Invalid file path while creating and writing in a Filejava.io.IOException:创建和写入文件时文件路径无效
【发布时间】:2019-08-07 10:03:34
【问题描述】:

我正在尝试在目录中创建一个文件,但是当代码执行时,它会返回错误 'java.io.IOException: Invalid file path'。 并且代码确实创建了名为“ServerUploads”的目录,但它不会创建文件。 下面是一个代码 sn-p :

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

作为参数传递的文件名是 'upload.txt' 。我不确定为什么它不起作用。任何帮助表示赞赏。谢谢!。 请参阅我需要 return String 而不是 void 的方法,因为我必须进一步将 return 发送给客户。

【问题讨论】:

  • 您好,您可以尝试在文件构造函数中切换\` to /` 吗?还有,字节数组是干什么用的?
  • 我已经用完整的方法更新了我的代码,不幸的是正斜杠不起作用。
  • @GavinEverett 您的代码与return sucess; 不同,它应该是String sucess =.... 或直接return "600 - The file has been successfully uploaded!"。您可以从控制台添加完整的异常以及您如何调用performUploadOperation(..,...)
  • 这里的createNewFile() 调用是浪费时间和空间,exists() 调用也是如此。

标签: java file java-io ioexception


【解决方案1】:

我已经找到了问题的解决方案。解决方案是将“.trim()”添加到文件名字符串中。文件进来时一定有一些空白。

【讨论】:

  • 欢迎来到 SO !很好,正如我所说,您的代码仅在我们希望看到的部分工作是您如何调用 performUploadOperation(.. , ..) 以及最终您传递的 filefilename !所以吸取教训对吗?下一次让我们在有问题的 SO 上添加更多上下文/相关性并节省每个人的时间。 :)
【解决方案2】:

我尝试通过将返回类型 String 更改为 void 来跟踪代码块,因为没有返回任何内容。有效。文件夹和文件都被创建。 这是相同的代码块:

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

从 main 调用上述函数:

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

【讨论】:

  • 您错过了提供变量类型。它应该是String sucess = 而不仅仅是sucess =。此外,文件的内容不会以您编写System.out.println(fos.toString()); 的方式打印它应该替换为:ByteArrayOutputStream stream = new ByteArrayOutputStream(); stream.write(file); System.out.println(new String(stream.toByteArray()));
  • 这里没有任何东西可以解决甚至识别问题。
  • 问题是java.io.IOException: Invalid file path 由发布的代码给出,我发布的答案解决了这个问题。当我在我的 m/c 上运行这个程序时,它没有给出任何错误。
猜你喜欢
  • 2013-12-08
  • 2020-07-09
  • 2011-02-19
  • 1970-01-01
  • 2020-11-30
  • 2012-03-16
  • 1970-01-01
相关资源
最近更新 更多