文件上傳報錯java.io.FileNotFoundException拒絕訪問


局部代碼如下:

1 File tempFile = new File("G:/tempfileDir"+"/"+fileName);
2         if(!tempFile.exists()){
3             tempFile.mkdirs();
4             System.out.println(tempFile.exists());
5         }

運行后發現,新建的目錄里面應該是個文件,但是確實個路徑

然后修改代碼如下

File tempFile = new File("G:/tempfileDir"+"/"+fileName);
        if(!tempFile.getParentFile().exists()){
            tempFile.getParentFile().mkdirs();//創建父級文件路徑
            tempFile.createNewFile();//創建文件
            System.out.println(tempFile.exists());
        }

報錯java.io.FileNotFoundException,導致拒絕訪問,往往是沒有相關文件或者是文件和路徑創建時調用方法調用錯了,需要注意。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM