这篇文章主要介绍“java怎么实现文件夹上传功能”,在日常操作中,相信很多人在java怎么实现文件夹上传功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java怎么实现文件夹上传功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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('');
})
}
这样做的好处是使用了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怎么实现文件夹上传功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/sunhoms/article/details/128345126