溫馨提示×

溫馨提示×

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

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

java怎么實(shí)現(xiàn)文件夾上傳功能

發(fā)布時間:2023-05-06 11:23:50 來源:億速云 閱讀:142 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“java怎么實(shí)現(xiàn)文件夾上傳功能”,在日常操作中,相信很多人在java怎么實(shí)現(xiàn)文件夾上傳功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java怎么實(shí)現(xiàn)文件夾上傳功能”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

一、前端如何設(shè)置上傳組件并將資源上傳到后臺服務(wù)

1)首先我們需要新建一個用來提交文件夾的form表單

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('');
      })
}

2)然后我們添加自己框架內(nèi)的一些按鈕來觸發(fā)該隱藏的表單

這樣做的好處是使用了form文件夾上傳的功能,卻不用使用他的UI

<!-- 首先創(chuàng)建一個按鈕用來觸發(fā)上傳事件 uploadSoundCodeBtn() -->
<el-button  v-loading="uploadSoundCodeLoading" 
@click="uploadSoundCodeBtn">
上傳文件夾
</el-button>
/*上傳事件觸發(fā)的方法*/
uploadSoundCodeBtn(){
  $("#fileFolder").click();
},

二、后臺如何接收處理文件夾表單數(shù)據(jù)

這里我們使用 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í)用的文章!

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

免責(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)容。

AI