亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:5日期: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 文件上傳頁(yè)面

<!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 上傳成功的頁(yè)面

<!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','上傳失敗,請(qǐng)選擇文件!');return 'index'; } String filename = file.getOriginalFilename(); //String filePath = request.getServletContext().getRealPath('/upload'); String filePath = ResourceUtils.getURL('classpath:').getPath()+'static/'; //避免文件重復(fù)覆蓋 String uuid= UUID.randomUUID().toString().replaceAll('-', ''); //時(shí)間戳分類文件 String time = new SimpleDateFormat('YYYY-MM').format(new Date()); String realPath = filePath+time+'/'+uuid+filename; File dest = new File(realPath); //檢測(cè)是否存在目錄,無(wú),則創(chuàng)建 if(!dest.getParentFile().exists()){dest.getParentFile().mkdirs();//新建文件夾 多級(jí)目錄 } 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; //猜測(cè) 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+'個(gè)文件失敗'); model.addAttribute('msgList',msgList); continue;}String filename = file.getOriginalFilename();//避免文件重復(fù)覆蓋String uuid= UUID.randomUUID().toString().replaceAll('-', '');//時(shí)間戳分類文件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);//檢測(cè)是否存在目錄,無(wú),則創(chuàng)建if(!dest.getParentFile().exists()){ dest.getParentFile().mkdirs();//新建文件夾 多級(jí)目錄}msgList.add('第'+i+'個(gè)文件,上傳成功!');file.transferTo(dest); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } model.addAttribute('msgList',msgList); return 'hello'; } }

測(cè)試:

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

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

計(jì)劃補(bǔ)充:文件下載+上傳的圖片展示;

上傳的圖片展示:

遇到的問(wèn)題: 直接使用 realPath 作為圖片拼接地址 瀏覽器報(bào) 安全錯(cuò)誤

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

使用字符串拼接,也會(huì)報(bào)錯(cuò)404

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久精品国内一区二区三区 | 免费一级毛片在播放视频 | 中文字幕在线播放 | 国产成人精品一区二区免费 | 99久久国产综合精品成人影院 | 日本高清不卡中文字幕 | 亚洲综合色区图片区 | 国产福利在线 | 国产大片喷水在线在线视频 | 成 人 黄 色 视频播放1 | 欧美一级特黄aaaaaaa在线观看 | 中国一级黄色录像片 | 麻豆精品视频 在线视频 | 欧美亚洲性色影视在线 | 亚洲精品免费在线 | 久久久国产在线 | 亚洲欧美精品日韩欧美 | 麻豆91制片厂 | 高清女主播一区二区三区 | 成人欧美一区二区三区小说 | 92香蕉视频 | 国产成人毛片精品不卡在线 | 国精品一区二区三区 | 91国偷自产一区二区三区蜜臀 | 日韩在线1| 国产精品suv| 欧美久在线观看在线观看 | 久久免费99精品久久久久久 | 精品久久成人免费第三区 | 综合精品视频 | 亚洲涩福利高清在线 | 国产精品久久久99 | 国产成+人+综合+亚洲欧美丁香花 | 国产成+人+综合+亚洲不卡 | 1024亚洲天堂 | 成人免费草草视频 | 91成人免费在线视频 | 午夜精品久久久久久 | 欧美日韩亚洲精品一区二区 | 国产精品免费一区二区三区四区 | 亚洲欧美日韩综合二区三区 |