解决上传文件提示java.io.IOException: java.io.FileNotFoundException:系统找不到指定的路径。 前端上传失败效果:后端对应的异常输出信息:此时后端对应的上传关键代码:原因分析:解决方案:
关键信息:
java.io.IOException: java.io.FileNotFoundException: C:\Users\brendon\AppData\Local\Temp\Tomcat.6510816303036534023.8099\work\Tomcat\localhost\ROOT\invoice\originalfile\2023-02-13\73432e18330dec9a05af2e74d068bfba83e0a88d.pdf (系统找不到指定的路径。)
Caused by: java.io.FileNotFoundException: C:\Users\brendon\AppData\Local\Temp\tomcat.6510816303036534023.8099\work\Tomcat\localhost\ROOT\invoice\originalfile\2023-02-13\73432e18330dec9a05af2e74d068bfba83e0a88d.pdf (系统找不到指定的路径。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.(FileOutputStream.java:213)
at java.io.FileOutputStream.(FileOutputStream.java:162)
at org.apache.tomcat.util.Http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:406)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:120)
… 92 more
//通过SHA1生成唯一文件名 String filename = hex.replaceAll("-","") + "." + suffix; String fullPath = savePath +"/"+ filename; System.out.println(fullPath); try { //将文件保存指定目录 file.transferTo(new File(fullPath)); } catch (Exception e) { e.printStackTrace(); resultView.setCode(ResultEnums.FaiLURE.getCode()); resultView.setMsg(ResultEnums.FAILURE.getMessage()+"保存文件异常"); return resultView; }
运行在保存文件 file.transferTo(new File(fullPath))处报错:
String fullPath = savePath +"/"+ filename;
是相对路径,指向invoice\originalfile\2023-02-13\73432e18330dec9a05af2e74d068bfba83e0a88d.pdf
file.transferTo 方法调用时,判断如果是相对路径,则使用temp目录,即C:\Users\brendon\AppData\Local\Temp\tomcat.6510816303036534023.8099\work\Tomcat\localhost\ROOT
位置不对,没有此目录存在,所以报错。
关键代码:
File currFile = new File(new File(savePath).getAbsolutePath()+"/" + filename);
file.transferTo(currFile);
至此解决问题。
来源地址:https://blog.csdn.net/H_Dsheng/article/details/129018163
--结束END--
本文标题: 上传文件提示java.io.IOException: java.io.FileNotFoundException:(系统找不到指定的路径。)
本文链接: https://www.lsjlt.com/news/371718.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0