springboot+thymeleaf 文件上传功能的实现代码
程序员文章站
2022-07-06 09:32:14
pom.xml org.springframework.boot
pom.xml
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
application.yml
spring: servlet: multipart: #上传文件总的最大值 max-request-size: 10mb #上传文件的最大值 max-file-size: 10mb
index.html 文件上传页面
<!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <title>文件上传</title> </head> <body> <p>单文件上传</p> <form method="post" action="/upload" enctype="multipart/form-data"> <p><input type="file" name="file00"></p> <p><span th:text="${msg}"></span></p> <input type="submit" value="提交"> </form> <hr/> <p>多文件上传</p> <form method="post" enctype="multipart/form-data" action="/batch"> <p>文件1:<input type="file" name="file"/></p> <p>文件2:<input type="file" name="file"/></p> <p><input type="submit" value="上传"/></p> </form> </body> </html>
hello.html 上传成功的页面
<!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <title>title</title> </head> <body> <p>单文件上传</p> <p th:text="${msg}"></p> <hr> <p>多文件上传</p> <ul> <li th:each="msg1:${msglist}" th:text="${msg1}"></li> </ul> </body> </html>
controller: 文件上传
import org.springframework.core.io.resourceloader; import org.springframework.http.responseentity; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.util.resourceutils; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.multipartfile; import org.springframework.web.multipart.multipartrequest; import javax.servlet.http.httpservletrequest; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.list; import java.util.uuid; @controller public class fileuploadcontroller { //单一文件上传 @requestmapping("/upload") public string uploadfile(@requestparam("file00") multipartfile file, model model){ string msg=""; try { if(file.isempty()){ model.addattribute("msg","上传失败,请选择文件!"); return "index"; } string filename = file.getoriginalfilename(); //string filepath = request.getservletcontext().getrealpath("/upload"); string filepath = resourceutils.geturl("classpath:").getpath()+"static/"; //避免文件重复覆盖 string uuid= uuid.randomuuid().tostring().replaceall("-", ""); //时间戳分类文件 string time = new simpledateformat("yyyy-mm").format(new date()); string realpath = filepath+time+"/"+uuid+filename; file dest = new file(realpath); //检测是否存在目录,无,则创建 if(!dest.getparentfile().exists()){ dest.getparentfile().mkdirs();//新建文件夹 多级目录 } file.transferto(dest);//文件写入 } catch (ioexception e) { e.printstacktrace(); } model.addattribute("msg","文件上传成功!"); return "hello"; } //多文件上传 @requestmapping("/batch") public string uploadmorefiles(httpservletrequest request, model model){ multipartrequest request1 = (multipartrequest)request; //猜测 file为 input 类型为 file list<multipartfile> filelist = request1.getfiles("file"); list<string> msglist = new arraylist<>(); system.out.println(filelist.size()); try { string filepath = resourceutils.geturl("classpath:").getpath()+"static/"; for (int i=1;i<=filelist.size();i++){ multipartfile file = filelist.get(i-1); if (file.isempty()){ msglist.add("上传第"+i+"个文件失败"); model.addattribute("msglist",msglist); continue; } string filename = file.getoriginalfilename(); //避免文件重复覆盖 string uuid= uuid.randomuuid().tostring().replaceall("-", ""); //时间戳分类文件 string time = new simpledateformat("yyyy-mm").format(new date()); string realpath = filepath+time+"s/"+uuid+filename; file dest = new file(realpath); //system.out.println("realpath"+realpath); //检测是否存在目录,无,则创建 if(!dest.getparentfile().exists()){ dest.getparentfile().mkdirs();//新建文件夹 多级目录 } msglist.add("第"+i+"个文件,上传成功!"); file.transferto(dest); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } model.addattribute("msglist",msglist); return "hello"; } }
测试:
注:目前仅实现了文件的上传
计划补充:文件下载+上传的图片展示;
上传的图片展示:
遇到的问题: 直接使用 realpath 作为图片拼接地址 浏览器报 安全错误
使用字符串拼接,也会报错404
index = realpath.lastindexof("static"); upfilepaths.add("../"+realpath.substring(index));
到此这篇关于springboot+thymeleaf 文件上传功能的实现代码的文章就介绍到这了,更多相关springboot thymeleaf 文件上传内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!