前台代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="../upload" method="post" enctype="multipart/form-data">
选择文件<input type="file" name="file">
<br>
<input type="submit" value="上传">
</form>
</body>
</html>
前台页面
后台java代码
@RequestMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
//这里需要注意的是ApplicationHome是属于SpringBoot的类
ApplicationHome applicationHome=new ApplicationHome(this.getClass());
System.out.println(applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath());
String path=applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+"\\src\\main\\webapp\\dome";
File files=new File(path,file.getOriginalFilename());
File parentFile = files.getParentFile();
if (!parentFile.exists()){
parentFile.mkdir();
}
file.transferTo(files);
return "success";
}
其中我输出System.out.println(applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath());的是项目在本地的代码结果为
D:\IdeaWorkSpace\springboot-study
执行成功之后就可以看到项目中新增一个文件加