MultipartFile.transferTo(dest) 报找不到文件错误以及解决方法
MultipartFile.transferTo(dest) 报找不到文件
今天使用transferTo这个方法进行上传文件的使用发现了一些路径的一些问题,查找了一下记录问题所在
前端上传网页,使用的是单文件上传的方式
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body><form enctype="multipart/form-data" method="post" action="/upload">文件:<input type="file" name="head_img">姓名:<input type="text" name="name"><input type="submit" value="上传"></form></body>
</html>
后台网页
@Controller
@RequestMapping("/file")
public class UploadFileController {undefined@Value("${file.upload.path}")private String path = "upload/";@RequestMapping(value = "fileUpload", method = RequestMethod.POST)@ResponseBodypublic String fileUpload(@RequestParam("file") MultipartFile file) {undefinedif (file.isEmpty()) {undefinedreturn "false";}String fileName = file.getOriginalFilename();File dest = new File(path + "/" + fileName);if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();}try {undefinedfile.transferTo(dest); // 保存文件return "true";} catch (Exception e) {undefinede.printStackTrace();return "false";}}
}
这个确实存在一些问题
路径是不对的
dest 是相对路径,指向 upload/doc20170816162034_001.jpg
file.transferTo 方法调用时,判断如果是相对路径,则使用temp目录,为父目录
因此,实际保存位置为 C:\Users\xxxx\AppData\Local\Temp\tomcat.372873030384525225.8080\work\Tomcat\localhost\ROOT\upload\doc20170816162034_001.jpg
所以改为:
@Controller
@RequestMapping("/file")
public class UploadFileController {undefined@Value("${file.upload.path}")private String path = "upload/";@RequestMapping(value = "fileUpload", method = RequestMethod.POST)@ResponseBodypublic String fileUpload(@RequestParam("file") MultipartFile file) {undefinedif (file.isEmpty()) {undefinedreturn "false";}String fileName = file.getOriginalFilename();File dest = new File(new File(path).getAbsolutePath()+ "/" + fileName);if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();}try {undefinedfile.transferTo(dest); // 保存文件return "true";} catch (Exception e) {undefinede.printStackTrace();return "false";}}
}
MultipartFile.transferTo(dest) 报找不到文件错误以及解决方法相关推荐
- 关于windows 找不到文件1的解决方法
关于"windows 找不到文件1"的解决方法 在开机的进入用户时会有些慢,并会跳出一个警告框,说文件"1"找不到. (应该是Windows下的1.com文件. ...
- linux服务器文件偶尔丢失,【服务器运维】linux抛出找不到文件非常的解决方法...
场景: 项目在windows下接见一般,linux下抛非常,找不到文件. 剖析: 假如接见的项目文件是如许的:abc/bcd/aa.jpg ,而体系中接见文件的途径是:abc/Bcd/aa.jpg,二 ...
- 关于nmonanalyser报错“输入超出文件尾”的解决方法
关于nmonanalyser报错"输入超出文件尾"的解决方法 运行环境:nmonanalyserV4.7 原因分析: 这个是工具的VBA宏报错,原因是*.nmon文件中 ...
- Linux中的基本命令无法使用,报Command not found的错误的解决方法
Linux中的基本命令无法使用,报Command not found的错误的解决方法 参考文章: (1)Linux中的基本命令无法使用,报Command not found的错误的解决方法 (2)ht ...
- 使用连接管理器出现“安装程序无法复制文件”错误的解决方法
使用连接管理器出现"安装程序无法复制文件"错误的解决方法 在我们的企业中,使用"连接管理器"创建的***客户端连接程序,在运行安装程序的时候,有的机器出现&qu ...
- MultipartFile.transferTo(dest) 报 FileNotFoundException
Spring Upload file 报错FileNotFoundException 环境: Springboot 2.0.4 JDK8 内嵌 Apache Tomcat/8.5.32 ...
- php.exe系统错误,PhpStorm中报 “Cannot run program git.exe, 系统找不到指定的文件” 错误的解决方法...
在使用PhpStorm的GitHub或Git功能时,经常会出现以下错误信息: Cannot run program "git.exe": CreateProcess error=2 ...
- W ndows找不到explorer,windows找不到文件explorer.exe解决方法
大部分的用户在进入系统后都遇到过windows找不到文件explorer.exe的情况只有电脑背景壁纸,那么这个问题该怎么解决呢?下面就给大家带来详细解决方法. windows找不到文件explore ...
- Android 编译报XML declaration not well-formed错误的解决方法
项目上一秒还运行得好好的,下一秒编译却突然不成功,报如下错误 FAILURE: Build failed with an exception.* What went wrong: Execution ...
最新文章
- 教你如何实现c#文件上传下载功能
- python与excel做数据可视化-python做可视化数据分析,究竟怎么样?
- Android中Gallery和ImageSwitcher的使用
- Java线程通俗讲解
- hexo部署云服务器的全过程
- this.counter$ = store.select(fromExample.getCounterCounter)之后马上subscribe
- java中常量final的用法_详解Java中final的用法
- 在 App 扩展和主 App 间共享数据
- 安卓工控主板运行时会自动重启_工控主板在工业自动化中的应用
- LeetCode 1854. 人口最多的年份(差分)
- Chapter2 MSP430硬件结构
- pyqt5中的lineEdit中只输入数字和字母
- 面试问题:SpringMVC的执行流程
- 【BZOJ3514】Codechef MARCH14 GERALD07加强版,LCT+主席树
- 《Excel 职场手册:260招菜鸟变达人》一第 26 招 对齐两个字的名字
- 旅行场景下的推荐算法探索
- scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
- 【细胞分割】基于matlab分水岭算法细胞分割计数【含Matlab源码 639期】
- 【转】赢在中国---马云点评创业精选
- 夏令营面试常见英文问题回答_回答有关5合1促销的3个常见问题