vue,Java上传文件并修改文件名,并保存到服务器(MultipartFile)

您所在的位置:网站首页 上传照片怎么改文件名 vue,Java上传文件并修改文件名,并保存到服务器(MultipartFile)

vue,Java上传文件并修改文件名,并保存到服务器(MultipartFile)

2023-09-09 17:01| 来源: 网络整理| 查看: 265

vue前端上传文件,Java 后台MultipartFile接收,修改名称后,保存到服务器 vue代码 项目部署到windows系统, 如果部署到Linux上,可能路径要转义一下 全局变量,获取配置文件 设置的保存 文件的服务器路径

@Value("${upload.paths}") private String filePaths;

yml 的路径配置

upload: paths: F:/shp

Springboot接口定义 前端上传一组文件,前台上传的变量为 files 返回值是我们项目封装的实体类,根据需要返回,也可以返回一个“success”的字符串

@PostMapping("upLoader") public BaseResponseVO uploadFile(@RequestParam("files") MultipartFile[] files) throws IOException {

项目中用到的地图图层数据(arcgis 中动态服务的文件)是一组同名的文件,后缀名不同

一定要有判空,开始没有判空,一直获取不到文件名 循环遍历文件,获取文件的后缀,新的文件名加后缀,组成保存到服务器的文件

//遍历上传的文件 for (MultipartFile file : files) { if (( file !=null && ! file.isEmpty() )) { String filename = file.getOriginalFilename(); String suffixName = filename.substring(filename.lastIndexOf(".")); filename = "ymzs"+suffixName; File dest = new File(filePaths +"/"+ filename); try { file.transferTo(dest); } catch (IOException e) { e.printStackTrace(); } } }

ok,修改文件名完成,并保存到服务器上了

上传的全部代码

@PostMapping("upLoader") public BaseResponseVO uploadFile(@RequestParam("files") MultipartFile[] files) throws IOException { //遍历上传的文件 for (MultipartFile file : files) { if (( file !=null && ! file.isEmpty() )) { String filename = file.getOriginalFilename(); String suffixName = filename.substring(filename.lastIndexOf(".")); filename = "ymzs"+suffixName; File dest = new File(filePaths +"/"+ filename); try { file.transferTo(dest); } catch (IOException e) { e.printStackTrace(); } } } return BaseResponseVO.success(); } vue代码 handleProgress() { const options = { text: '正在上传,请稍候...' } this.loadingInstance = Loading.service(options) }, handleSuccess(res, file, projectData) { console.log(res, file) this.fileName = res.data.split('.zip')[0] this.fileName1 = this.fileName console.log('上传项目文件名称', this.fileName) if (res.message == 'success') { this.returnFlag(projectData) // this.beforeUploadHandle(projectData) } else { this.message.error({ title: '信息提示', content: '上传失败,请检查重新上传shp文件', desc: '' }) } }, handleChange(file, fileList) { console.log(file, fileList) this.fileName = file.name.substring(0, file.name.lastIndexOf('.')) this.formLabelAlign.fileName = this.fileName console.log(this.fileList, this.fileName) var data = { fileName: this.fileName } this.$refs.projectUpload.submit() this.$refs.uploadFormItem.clearValidate() },


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3