[XML] 纯文本查看 复制代码
<!--配置父级工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<!--配置WEB启动器 SpringMVC、Restful、jackson-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置springboot热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加thymeleaf-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<!--配置全局属性-->
<properties>
<!--配置jdk版本-->
<java.version>1.8</java.version>
</properties>
<!--配置maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html>
<head>
<title>uploadFiles.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
<meta name="description" content="this is my page"></meta>
<meta name="content-type" content="text/html; charset=UTF-8"></meta>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/testuploadimg">
图片<input type="file" name="file"/>
<input type="submit" value="上传图片"/>
</form>
</body>
</body>
</html>
[Java] 纯文本查看 复制代码
@GetMapping("/toUpload")
public String turnToUploadHtml(){
return "/html/uploadFiles.html";
}
@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
public @ResponseBody
String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
try {
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
e.printStackTrace();
}
//返回json
return "success";
}