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

Spring Boot文件上傳簡單實例代碼

 更新時間:2019年08月21日 08:23:14   作者:程序魚  
在本篇文章里小編給大家分享的是關(guān)于Spring Boot 文件上傳簡易教程以及相關(guān)知識點,需要的朋友們參考下。

上傳文件是我們?nèi)粘J褂米顬閺V泛的功能之一,比如App端上傳頭像。本章演示如何從客戶端上傳到 Spring Boot 開發(fā)的 Api中。

https://github.com/fishpro/spring-boot-study/tree/master/spring-boot-study-upload

1 新建 Spring Boot Maven 示例工程項目

注意:本示例是用 IDEA 開發(fā)工具

  • File > New > Project,如下圖選擇 Spring Initializr 然后點擊 【Next】下一步
  • 填寫 GroupId(包名)、Artifact(項目名) 即可。點擊 下一步
  • groupId=com.fishpro
  • artifactId=upload
  • 選擇依賴 Spring Web Starter 前面打鉤。
  • 項目名設(shè)置為 spring-boot-study-upload.

文件上傳不需要引入第三方組件。

2 依賴引入 Pom.xml

為了演示代碼,這里引入 thymeleaf

<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

3 編寫上傳示例
本章代碼主要演示單文件上傳和多文件上傳,前端采用 thymeleaf 模板,實際上就是一個html文件。文件清單包括

  • uploadfile.html 前端文件
  • FileController.java 控制層文件
  • FileUtil.java 文件常用類

3.1 控制層代碼

主要使用 MultipartFile 來實現(xiàn),如下代碼 /upload 和 /uploads 分別為單文件上傳和多文件上傳。其中 @Value("${fishpro.uploadPath}") 是配置文件中設(shè)置的。

server.port=8086
fishpro.uploadPath=/Users/jiaojunkang/Desktop/upload/
@Controller
public class FileController {

 @Value("${fishpro.uploadPath}")
 private String uploadPath;

 @GetMapping("/uploadfile")
 public String uploadfile(){
  return "uploadfile";
 }


 /**
  * 單文件
  * */
 @PostMapping("/upload")
 @ResponseBody
 Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
  Map<String,Object> map=new HashMap();
  map.put("status",0);
  String fileName = file.getOriginalFilename();
  fileName = UUID.randomUUID().toString(); //對文件名稱重命名

  try {
   FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
   map.put("filename",fileName);
  } catch (Exception e) {
   map.put("status",-1);
   map.put("message",e.getMessage());

  }


  return map;
 }

 /**
  * 多文件
  * */
 @PostMapping("/uploads")
 @ResponseBody
 Object uploads(@RequestParam("files") MultipartFile [] files, HttpServletRequest request) {
  Map<String,Object> map=new HashMap();
  map.put("status",0);
  List<String> filenames=new ArrayList<>();
  for (MultipartFile file: files
    ) {
   String ext = file.getOriginalFilename().split("\\.")[1];
   String fileName = file.getOriginalFilename();
   //fileName = UUID.randomUUID().toString()+"."+ext; //對文件名稱重命名

   try {
    FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
    filenames.add(fileName);
   } catch (Exception e) {
    map.put("status",-1);
    map.put("message",e.getMessage());
    return map;

   }
  }

  map.put("filename",filenames);
  return map;
 }
}

3.2 前端文件

前端文件這里演示的比較簡單,可以有多中形態(tài),這里使用 form 來提交。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<hr/>
<div>單位文件</div>
<form enctype="multipart/form-data" method="post" action="/upload">
  文件 <input type="file" name="file"/>
  <input type="submit" value="上傳"/>
</form>
<hr/>
<div>多文件</div>

<form enctype="multipart/form-data" method="post" action="/uploads">
  <p>文件1<input type="file" name="files"/></p>
  <p>文件2<input type="file" name="files"/></p>
  <p>文件3<input type="file" name="files"/></p>
  <p><input type="submit" value="上傳"/></p>
</form>
</body>
</html>

3.3 文件保存類

public class FileUtil {

  public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
    File targetFile = new File(filePath);
    if (!targetFile.exists()) {
      targetFile.mkdirs();
    }
    FileOutputStream out = new FileOutputStream(filePath + fileName);
    out.write(file);
    out.flush();
    out.close();
  }

  public static boolean deleteFile(String fileName) {
    File file = new File(fileName);
    // 如果文件路徑所對應(yīng)的文件存在,并且是一個文件,則直接刪除
    if (file.exists() && file.isFile()) {
      if (file.delete()) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  public static String renameToUUID(String fileName) {
    return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
  }
}

以上就是本次介紹的全部知識點內(nèi)容,感謝大家的閱讀和對腳本之家的支持。

相關(guān)文章

  • java字節(jié)流知識點總結(jié)

    java字節(jié)流知識點總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于java字節(jié)流的相關(guān)知識點內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)參考下。
    2019-07-07
  • spring-boot-maven-plugin報紅解決方案(親測有效)

    spring-boot-maven-plugin報紅解決方案(親測有效)

    本文主要介紹了spring-boot-maven-plugin報紅解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 你知道怎么用Spring的三級緩存解決循環(huán)依賴嗎

    你知道怎么用Spring的三級緩存解決循環(huán)依賴嗎

    這篇文章主要為大家詳細介紹了Spring的三級緩存解決循環(huán)依賴,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • java swing實現(xiàn)貪吃蛇雙人游戲

    java swing實現(xiàn)貪吃蛇雙人游戲

    這篇文章主要為大家詳細介紹了java swing實現(xiàn)貪吃蛇雙人小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 使用feign配置網(wǎng)絡(luò)ip代理

    使用feign配置網(wǎng)絡(luò)ip代理

    這篇文章主要介紹了使用feign配置網(wǎng)絡(luò)ip代理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Idea中g(shù)it的使用小結(jié)

    Idea中g(shù)it的使用小結(jié)

    這篇文章主要介紹了Idea中g(shù)it的使用小結(jié),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • Spring?Boot?實現(xiàn)字段唯一校驗功能(實例代碼)

    Spring?Boot?實現(xiàn)字段唯一校驗功能(實例代碼)

    這篇文章主要介紹了Spring?Boot?實現(xiàn)字段唯一校驗,實現(xiàn)代碼很簡單,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • Java使用Sharding-JDBC分庫分表進行操作

    Java使用Sharding-JDBC分庫分表進行操作

    Sharding-JDBC 是無侵入式的 MySQL 分庫分表操作工具,本文主要介紹了Java使用Sharding-JDBC分庫分表進行操作,感興趣的可以了解一下
    2021-08-08
  • springboot配置mybatis和事務(wù)管理方式

    springboot配置mybatis和事務(wù)管理方式

    這篇文章主要介紹了springboot配置mybatis和事務(wù)管理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java+Springboot搭建一個在線網(wǎng)盤文件分享系統(tǒng)

    Java+Springboot搭建一個在線網(wǎng)盤文件分享系統(tǒng)

    本主要介紹了通過springboot+freemark+jpa+MySQL實現(xiàn)的在線網(wǎng)盤文件分享系統(tǒng),其功能跟百度網(wǎng)盤非常類似,可以實現(xiàn)文件的上傳、移動、復(fù)制、下載等,需要的可以參考一下
    2021-11-11

最新評論