您好,登錄后才能下訂單哦!
這篇文章主要介紹“java怎么實(shí)現(xiàn)文件夾上傳功能”,在日常操作中,相信很多人在java怎么實(shí)現(xiàn)文件夾上傳功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java怎么實(shí)現(xiàn)文件夾上傳功能”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
1.添加一個 type=file 的 input 提交組件,添加 webkitdirectory 標(biāo)識來使用文件夾上傳功能
2.添加 @change=“uploadSoundCodeFolder” 事件,當(dāng)我們上傳了文件夾后將觸發(fā) uploadSoundCodeFolder() 函數(shù)來處理上傳邏輯
<form id="uploadSoundCodeFolderForm" method="post" enctype="multipart/form-data"> <input id="fileFolder" name="fileFolder" type="file" @change="uploadSoundCodeFolder" webkitdirectory> </form>
uploadSoundCodeFolder() 實(shí)現(xiàn)邏輯如下
uploadSoundCodeFolder(e){ this.uploadSoundCodeLoading = true; //獲取到選中的文件夾內(nèi)的所有文件 //files 為一個集合 //可通過遍歷 files 的方式獲取到每個文件的大小等數(shù)據(jù),來實(shí)現(xiàn)大小限制等需求 let files = e.target.files; //中間省略大小限制等需求...... //獲取表單數(shù)據(jù) let formData = new FormData(document.getElementById("uploadSoundCodeFolderForm")); //調(diào)用后臺服務(wù)方法來提交該表單數(shù)據(jù) uploadSoundCode(formData).then((res)=>{ _this.$message.success("上傳成功") //上傳成功后清空表單數(shù)據(jù) $("#fileFolder").val(''); }) }
這樣做的好處是使用了form文件夾上傳的功能,卻不用使用他的UI
<!-- 首先創(chuàng)建一個按鈕用來觸發(fā)上傳事件 uploadSoundCodeBtn() --> <el-button v-loading="uploadSoundCodeLoading" @click="uploadSoundCodeBtn"> 上傳文件夾 </el-button>
/*上傳事件觸發(fā)的方法*/ uploadSoundCodeBtn(){ $("#fileFolder").click(); },
這里我們使用 List fileFolde 類型來接受前端發(fā)來的文件集合,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); }
然后根據(jù)業(yè)務(wù)將文件保存到服務(wù)器就行了
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 ""; }
到此,關(guān)于“java怎么實(shí)現(xiàn)文件夾上傳功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。