最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年11月25日 14:31:34   作者:weize_hh  
這篇文章主要介紹了springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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);
      //檢測是否存在目錄,無,則創(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";
  }
 
}

測試:

文件上傳文件上傳2文件上傳3

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

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

上傳的圖片展示:

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

notallow

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

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

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

相關(guān)文章

  • JVM鉤子函數(shù)的使用場景詳解

    JVM鉤子函數(shù)的使用場景詳解

    當jvm進程退出的時候,或者受到了系統(tǒng)的中斷信號,hook線程就會啟動,一個線程可以注入多個鉤,下面這篇文章主要給大家介紹了關(guān)于JVM鉤子函數(shù)使用的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Java8新特性 StreamAPI實例詳解

    Java8新特性 StreamAPI實例詳解

    這篇文章主要為大家介紹了Java8新特性 StreamAPI實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • java 讀取網(wǎng)頁內(nèi)容的實例詳解

    java 讀取網(wǎng)頁內(nèi)容的實例詳解

    這篇文章主要介紹了java 讀取網(wǎng)頁內(nèi)容的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家學習理解這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • JAVA Stack詳細介紹和示例學習

    JAVA Stack詳細介紹和示例學習

    JAVA Stack是棧。它的特性是:先進后出(FILO, First In Last Out)。
    2013-11-11
  • SpringBoot實現(xiàn)模塊日志入庫的項目實踐

    SpringBoot實現(xiàn)模塊日志入庫的項目實踐

    本文主要介紹了SpringBoot實現(xiàn)模塊日志入庫的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Java中如何實現(xiàn)不可變Map詳解

    Java中如何實現(xiàn)不可變Map詳解

    這篇文章主要給大家介紹了關(guān)于Java中如何實現(xiàn)不可變Map的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作工具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • SpringBoot框架集成token實現(xiàn)登錄校驗功能

    SpringBoot框架集成token實現(xiàn)登錄校驗功能

    這篇文章主要為大家詳細介紹了SpringBoot框架集成token實現(xiàn)登錄校驗功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 用Java輕松讀取Word文檔內(nèi)容的常用方法

    用Java輕松讀取Word文檔內(nèi)容的常用方法

    這篇文章主要介紹了用Java輕松讀取Word文檔內(nèi)容的常用方法,對于doc格式使用Apache?POI庫中的HWPFDocument和WordExtractor類,對于docx格式使用XWPFDocument類,并通過遍歷段落和文本運行對象來提取文本內(nèi)容,需要的朋友可以參考下
    2025-03-03
  • Java1.8中LocalDate方法使用總結(jié)

    Java1.8中LocalDate方法使用總結(jié)

    LocalDate是Java8中的一個日期類,用于表示年月日,它是不可變的,線程安全的,下面這篇文章主要給大家介紹了關(guān)于Java1.8中LocalDate方法使用的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • idea不能自動補全yml配置文件的原因分析

    idea不能自動補全yml配置文件的原因分析

    這篇文章主要介紹了idea不能自動補全yml配置文件的原因,通過添加yml文件為配置文件能夠很快的解決,具體解決步驟跟隨小編一起通過本文學習下吧
    2021-06-06

最新評論

南召县| 襄樊市| 武安市| 温宿县| 玛沁县| 宕昌县| 成安县| 黑龙江省| 武鸣县| 阜平县| 武邑县| 铜鼓县| 新密市| 恩平市| 柞水县| 易门县| 永顺县| 遂溪县| 东乡县| 黎城县| 页游| 元谋县| 方城县| 大宁县| 青阳县| 满城县| 砚山县| 甘谷县| 集贤县| 丹凤县| 桃江县| 汾西县| 东方市| 东丰县| 开江县| 磐安县| 雅江县| 新蔡县| 青田县| 雷波县| 昌黎县|