Home > Java > javaTutorial > body text

How to implement folder upload function in java

PHPz
Release: 2023-05-10 20:52:04
forward
2716 people have browsed it

1. How to set up the upload component on the front end and upload resources to the background service

1) First we need to create a new form for submitting folders

1. Add a type= In the input submission component of file, add the webkitdirectory logo to use the folder upload function

2. Add the @change="uploadSoundCodeFolder" event. When we upload the folder, the uploadSoundCodeFolder() function will be triggered to handle the upload logic

<form id="uploadSoundCodeFolderForm" 
 method="post" 
enctype="multipart/form-data">
  <input id="fileFolder" name="fileFolder" type="file" 
 		@change="uploadSoundCodeFolder" webkitdirectory>
</form>
Copy after login

uploadSoundCodeFolder() The implementation logic is as follows

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(&#39;&#39;);
      })
}
Copy after login

2) Then we add some buttons in our own frame to trigger the hidden form

The advantage of this is that we use form Folder upload function without using its UI

<!-- 首先创建一个按钮用来触发上传事件 uploadSoundCodeBtn() -->
<el-button  v-loading="uploadSoundCodeLoading" 
@click="uploadSoundCodeBtn">
上传文件夹
</el-button>
Copy after login
/*上传事件触发的方法*/
uploadSoundCodeBtn(){
  $("#fileFolder").click();
},
Copy after login

2. How to receive and process folder form data in the background

Here we use the List fileFolde type to accept the file collection sent by the front end. fileFolde is the name in the form

@RequestMapping(value="/uploadSoundCode",method= RequestMethod.POST)
public AjaxResult uploadSoundCode(List<MultipartFile> fileFolde) throws IOException {
        String soundCodeUrl = HereUtil.uploadSoundCode(fileFolder);
        return AjaxResult.success(soundCodeUrl);
    }
Copy after login

Then just save the file to the server according to the business

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 "";
    }
Copy after login

The above is the detailed content of How to implement folder upload function in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template