溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot中怎么實(shí)現(xiàn)多文件上傳功能

發(fā)布時(shí)間:2021-08-07 13:52:15 來源:億速云 閱讀:140 作者:Leah 欄目:編程語言

springboot中怎么實(shí)現(xiàn)多文件上傳功能,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

首先創(chuàng)建一個(gè)springboot項(xiàng)目,添加spring-boot-starter-web依賴。

然后在resources下的static文件夾下創(chuàng)建uploads.html文件,文件內(nèi)容如下:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>多文件上傳</title></head><body><form action="/uploads" method="post" enctype="multipart/form-data">  <input type="file" name="uploadFiles" value="請(qǐng)選擇文件" multiple>  <input type="submit" value="上傳"></form></body></html>

然后編寫Controller類

@RestControllerpublic class FilesUploadController {   SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");   @RequestMapping("/uploads")  public String upload(MultipartFile[] uploadFiles, HttpServletRequest request) {    List list = new ArrayList();//存儲(chǔ)生成的訪問路徑    if (uploadFiles.length > 0) {      for (int i = 0; i < uploadFiles.length; i++) {        MultipartFile uploadFile = uploadFiles[i];        //設(shè)置上傳文件的位置在該項(xiàng)目目錄下的uploadFile文件夾下,并根據(jù)上傳的文件日期,進(jìn)行分類保存        String realPath = request.getSession().getServletContext().getRealPath("uploadFile");        String format = sdf.format(new Date());        File folder = new File(realPath + format);        if (!folder.isDirectory()) {          folder.mkdirs();        }         String oldName = uploadFile.getOriginalFilename();        System.out.println("oldName = " + oldName);        String newName = UUID.randomUUID().toString() + oldName.            substring(oldName.lastIndexOf("."), oldName.length());        System.out.println("newName = " + newName);        try {          //保存文件          uploadFile.transferTo(new File(folder, newName));           //生成上傳文件的訪問路徑          String filePath = request.getScheme() + "://" + request.getServerName() + ":"+ request.getServerPort() + "/uploadFile" + format + newName;          list.add(filePath);        } catch (IOException e) {          e.printStackTrace();        }      }      return list.toString();    } else if (uploadFiles.length == 0) {      return "請(qǐng)選擇文件";    }    return "上傳失敗";  }}

相比于單文件上傳,這里就多了一個(gè)遍歷的過程。

文件上傳常見配置:

#是否開啟文件上傳支持,默認(rèn)是truespring.servlet.multipart.enabled=true #文件寫入磁盤的閾值,默認(rèn)是0spring.servlet.multipart.file-size-threshold=0#上傳文件的臨時(shí)保存位置spring.servlet.multipart.location=D:\\upload#單個(gè)文件的最大值,默認(rèn)是1MBspring.servlet.multipart.max-file-size=1MB#多個(gè)文件上傳時(shí)的總大小 值,默認(rèn)是10MBspring.servlet.multipart.max-request-size=10MB#是否延遲解析,默認(rèn)是falsespring.servlet.multipart.resolve-lazily=false

關(guān)于springboot中怎么實(shí)現(xiàn)多文件上傳功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI