2021SC@SDUSC
1)添加thymeleaf
<!-- 添加thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2).在 src/main/resources 目录下新建 static 目录和 templates 目录。 static存放静态文件,比如 css、js、image… templates 存放静态页面。先在templates 中新建一个 uploadimg.html
<!DOCTYPE html>
<html>
<head>
<title>uploadimg.html</title>
<span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span><span class="hljs-tag"></<span class="hljs-title">meta</span>></span>
<span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"this is my page"</span>></span><span class="hljs-tag"></<span class="hljs-title">meta</span>></span>
<span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"content-type"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"text/html; charset=UTF-8"</span>></span><span class="hljs-tag"></<span class="hljs-title">meta</span>></span>
<span class="hljs-comment"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></span>
</head>
<body>
<form enctype=“multipart/form-data” method=“post” action="/testuploadimg">
图片<input type=“file” name=“file”/>
<input type=“submit” value=“上传”/>
</form>
</body>
</html>
3).在 controller 中写两个方法,一个方法跳转到上传文件的页面,一个方法处理上传文件
//跳转到上传文件的页面
@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳转到 templates 目录下的 uploadimg.html
return "uploadimg";
}
<span class="hljs-comment">//处理文件上传</span>
@RequestMapping(<span class="hljs-keyword">value</span>=<span class="hljs-string">"/testuploadimg"</span>, method = RequestMethod.POST)
<span class="hljs-keyword">public</span> @ResponseBody String <span class="hljs-title">uploadImg</span>(@<span class="hljs-title">RequestParam</span>("file") MultipartFile file,
HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
<span class="hljs-comment">/*System.out.println("fileName-->" + fileName);
System.out.println("getContentType-->" + contentType);*/</span>
String filePath = request.getSession().getServletContext().getRealPath(<span class="hljs-string">"imgupload/"</span>);
<span class="hljs-keyword">try</span> {
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
} <span class="hljs-keyword">catch</span> (Exception e) {
<span class="hljs-comment">// TODO: handle exception</span>
}
<span class="hljs-comment">//返回json</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"uploadimg success"</span>;
}</code></pre>
4).在上面中,我将文件上传的实现写在工具类 FileUtil 的 uploadFile 方法中
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
5).在浏览器输入 :http://localhost:8080/gouploadimg 测试
上传文件后:
在应用的 src/main/webapp/imgupload 目录下
6).如果上传的文件大于 1M 时,上传会报错文件太大的错误,在 application.properties 中设置上传文件的参数即可
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb