SpringBoot文件上傳簡單實例代碼

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

十年專注成都網(wǎng)站制作,成都定制網(wǎng)頁設(shè)計,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)頁設(shè)計,高端網(wǎng)頁制作,對成都花箱等多個方面,擁有豐富的網(wǎng)站維護(hù)經(jīng)驗。

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)容,感謝大家的閱讀和對創(chuàng)新互聯(lián)的支持。

分享名稱:SpringBoot文件上傳簡單實例代碼
鏈接分享:http://muchs.cn/article42/pisdec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司品牌網(wǎng)站建設(shè)、企業(yè)建站、關(guān)鍵詞優(yōu)化ChatGPT、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管