欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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/";
 
      //避免文件重復(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);
      //檢測是否存在目錄,無,則創(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+"個(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);
        //檢測是否存在目錄,無,則創(chuàng)建
        if(!dest.getParentFile().exists()){
          dest.getParentFile().mkdirs();//新建文件夾 多級目錄
        }
        msgList.add("第"+i+"個(gè)文件,上傳成功!");
        file.transferTo(dest);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    model.addAttribute("msgList",msgList);
    return "hello";
  }
 
}

測試:

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

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

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

上傳的圖片展示:

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

notallow

使用字符串拼接,也會(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解讀Spring配置與服務(wù)組件的關(guān)系和注入機(jī)制

    解讀Spring配置與服務(wù)組件的關(guān)系和注入機(jī)制

    這篇文章主要介紹了解讀Spring配置與服務(wù)組件的關(guān)系和注入機(jī)制,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Spring?Boot常用功能Profile詳解

    Spring?Boot常用功能Profile詳解

    SpringBootProfile是一個(gè)很常用的功能,我們可以通過為開發(fā)/測試/生產(chǎn)環(huán)境配置不同的profile來實(shí)現(xiàn)配置隔離,那么在SpringBoot項(xiàng)目中是如何實(shí)現(xiàn)profile功能的呢
    2022-07-07
  • maven如何動(dòng)態(tài)統(tǒng)一修改版本號的方法步驟

    maven如何動(dòng)態(tài)統(tǒng)一修改版本號的方法步驟

    這篇文章主要介紹了maven如何動(dòng)態(tài)統(tǒng)一修改版本號的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • java實(shí)現(xiàn)打印日歷

    java實(shí)現(xiàn)打印日歷

    這篇文章主要為大家詳細(xì)介紹了java打印日歷的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 關(guān)于Java反射給泛型集合賦值問題

    關(guān)于Java反射給泛型集合賦值問題

    這篇文章主要介紹了Java反射給泛型集合賦值,需要的朋友可以參考下
    2022-01-01
  • Java使用poi生成word文檔的簡單實(shí)例

    Java使用poi生成word文檔的簡單實(shí)例

    Java POI是一個(gè)用于處理Microsoft Office文件(如Word、Excel和PowerPoint)的API,它是一個(gè)開源庫,允許Java開發(fā)者讀取、創(chuàng)建和修改這些文檔,本文給大集介紹了Java使用poi生成word文檔的簡單實(shí)例,感興趣的朋友可以參考下
    2024-06-06
  • SpringBoot中到底該如何解決跨域問題

    SpringBoot中到底該如何解決跨域問題

    跨域問題更是老生常談,隨便用標(biāo)題去google或百度一下,能搜出一大片解決方案,這篇文章主要給大家介紹了關(guān)于SpringBoot中到底該如何解決跨域問題的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • springboot讀取resources下文件的方式詳解

    springboot讀取resources下文件的方式詳解

    最近寫讀取模板文件做一些后續(xù)的處理,將文件放在了項(xiàng)目的resources下,發(fā)現(xiàn)了一個(gè)好用的讀取方法,下面這篇文章主要給大家介紹了關(guān)于springboot讀取resources下文件的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Springboot整合Dubbo+Nacos實(shí)現(xiàn)RPC調(diào)用的示例代碼

    Springboot整合Dubbo+Nacos實(shí)現(xiàn)RPC調(diào)用的示例代碼

    隨著互聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,越來越多的企業(yè)和開發(fā)者開始關(guān)注微服務(wù)架構(gòu),Nacos是阿里巴巴開源的一個(gè)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺,本文講解如何將Spring Boot與Dubbo和Nacos整合,實(shí)現(xiàn)RPC調(diào)用,需要的朋友可以參考下
    2024-02-02
  • idea自定義快捷代碼生成模板的方法

    idea自定義快捷代碼生成模板的方法

    這篇文章主要介紹了idea自定義快捷代碼生成模板的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評論