电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼

瀏覽:9日期:2023-04-06 10:54:47

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; @Controllerpublic 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); //檢測是否存在目錄,無,則創(chuàng)建 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);//檢測是否存在目錄,無,則創(chuàng)建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'; } }

測試:

springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼

注:目前僅實現(xiàn)了文件的上傳

計劃補充:文件下載+上傳的圖片展示;

上傳的圖片展示:

遇到的問題: 直接使用 realPath 作為圖片拼接地址 瀏覽器報 安全錯誤

springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼

使用字符串拼接,也會報錯404

index = realPath.lastIndexOf('static');upFilePaths.add('../'+realPath.substring(index));

到此這篇關于springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼的文章就介紹到這了,更多相關springboot thymeleaf 文件上傳內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關文章:
主站蜘蛛池模板: 真丝围巾|真丝丝巾|羊绒围巾|围巾品牌|浙江越缇围巾厂家定制 | 洗砂机械-球磨制砂机-洗沙制砂机械设备_青州冠诚重工机械有限公司 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 圆窗水平仪|伊莉莎冈特elesa+ganter | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 北京开源多邦科技发展有限公司官网 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 球磨机 选矿球磨机 棒磨机 浮选机 分级机 选矿设备厂家 | 北京公寓出租网-北京酒店式公寓出租平台 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 苏州柯瑞德货架-仓库自动化改造解决方案 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 衬氟旋塞阀-卡套旋塞阀-中升阀门首页 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | crm客户关系管理系统,销售管理系统,crm系统,在线crm,移动crm系统 - 爱客crm | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 光环国际-新三板公司_股票代码:838504 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 全自动固相萃取仪_高通量真空平行浓缩仪-勤业永为 | 电主轴-高速精密电主轴-高速电机厂家-瑞德沃斯品牌有限公司 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 洗瓶机厂家-酒瓶玻璃瓶冲瓶机-瓶子烘干机-封口旋盖压盖打塞机_青州惠联灌装机械 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 小型数控车床-数控车床厂家-双头数控车床|