java怎麼實作資料夾上傳功能
springboot
一、前端如何設定上傳元件並將資源上傳到後台服務
1)首先我們需要新建一個用來提交資料夾的form表單
1.新增一個type= file 的input 提交元件,新增webkitdirectory 識別來使用資料夾上傳功能
2.新增@change=「uploadSoundCodeFolder」 事件,當我們上傳了資料夾後將觸發uploadSoundCodeFolder() 函數來處理上傳邏輯
<form id="uploadSoundCodeFolderForm" method="post" enctype="multipart/form-data"> <input id="fileFolder" name="fileFolder" type="file" @change="uploadSoundCodeFolder" webkitdirectory> </form>
登入後複製
uploadSoundCodeFolder() 實作邏輯如下
uploadSoundCodeFolder(e){ this.uploadSoundCodeLoading = true; //获取到选中的文件夹内的所有文件 //files 为一个集合 //可通过遍历 files 的方式获取到每个文件的大小等数据,来实现大小限制等需求 let files = e.target.files; //中间省略大小限制等需求...... //获取表单数据 let formData = new FormData(document.getElementById("uploadSoundCodeFolderForm")); //调用后台服务方法来提交该表单数据 uploadSoundCode(formData).then((res)=>{ _this.$message.success("上传成功") //上传成功后清空表单数据 $("#fileFolder").val(''); }) }
登入後複製
2)然後我們加入自己框架內的一些按鈕來觸發該隱藏的表單
這樣做的好處是使用了form資料夾上傳的功能,卻不用使用他的UI
<!-- 首先创建一个按钮用来触发上传事件 uploadSoundCodeBtn() --> <el-button v-loading="uploadSoundCodeLoading" @click="uploadSoundCodeBtn"> 上传文件夹 </el-button>
登入後複製
/*上传事件触发的方法*/ uploadSoundCodeBtn(){ $("#fileFolder").click(); },
登入後複製
二、後台如何接收處理資料夾表單資料
這裡我們使用List fileFolde 類型來接受前端發送的檔案集合, fileFolde為表單裡面的name
@RequestMapping(value="/uploadSoundCode",method= RequestMethod.POST) public AjaxResult uploadSoundCode(List<MultipartFile> fileFolde) throws IOException { String soundCodeUrl = HereUtil.uploadSoundCode(fileFolder); return AjaxResult.success(soundCodeUrl); }
登入後複製
然後根據業務將檔案儲存到伺服器就行了
public static String uploadSoundCode(List<MultipartFile> files) throws IOException { for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); if (StrUtil.isBlank(fileName)){ continue; } //上传后的URL全路径 String fullFilePath = "上传的跟路径" + fileName; FileUtil.writeFromStream(file.getInputStream(), fullFilePath); } return ""; }
登入後複製
以上是java怎麼實作資料夾上傳功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Springboot+Mybatis-plus不使用SQL語句進行多表新增怎麼實現
