网站开发进度缓慢,做服装搭配直接售卖的网站,搜索引擎优化的含义,网站建设中模板springboot 实现本地文件存储 实现过程
上传文件保存文件#xff08;本地磁盘#xff09;返回文件HTTP访问服务器路径给前端#xff0c;进行效果展示
存储
服务端接收上传的目的是提供文件的访问服务#xff0c;对于SpringBoot而言#xff0c;其对静态资源访问提供了很…springboot 实现本地文件存储 实现过程
上传文件保存文件本地磁盘返回文件HTTP访问服务器路径给前端进行效果展示
存储
服务端接收上传的目的是提供文件的访问服务对于SpringBoot而言其对静态资源访问提供了很好的支持使用其提供的基本默认配置可以满足开发需求同时又支持开发人员进行自定义配置。 SpringBoot默认将 / 所有访问映射到以下目录** classpath:/META-INF/resourcesclasspath:/staticclasspath:/publicclasspath:/resources SpringBoot默认会挨个从pubic、resources、static里面找是否存在相应的资源如果有则直接返回。
问题
如果都放在classpath目录下打包的文件就会很大代码与文件数据不能分开存储就意味着文件数据的备份将变得复杂
解决方法
springboot提供了 spring.resources.static-locations 配置自定义静态文件的位置 注该配置有问题在下面以解决
spring:web:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
# 设置Http能访问的本地资源路径
demo:web:upload-path: D:/MineFile/zuoye/xm/equipment-management-system/qhjdata/配置 demo.web.upload-path 为与项目代码分离的静态资源路径即文件上传保存根路径配置 spring.web.resources.static-locations 除了带上SpringBoot默认的静态资源路径之外加上file:${demo.web.upload-path}指向外部的文件资源上传路径即该路径下的静态资源可以直接对外提供HTTP访问服务 /*** 本地上传* param file* param request* return*/RequestMapping(/file)public R fileSave(MultipartFile file, HttpServletRequest request) {if (file null) {throw new RRException(参数为空);}// 在 uploadPath 文件夹中通过日期对上传的文件归类保存// 例如/2022/02/22/df9a66f1-760b-4f95-9faf-b5a216966718.pngString format sdf.format(new Date());File folder new File(uploadPath format);if (!folder.isDirectory()) {folder.mkdirs();}// 对上传的文件重命名, 避免文件重名String oldName file.getOriginalFilename();String newName UUID.randomUUID().toString() oldName.substring(oldName.lastIndexOf(.), oldName.length());try {// 文件保存file.transferTo(new File(folder, newName));// 添加日志输出logger.info(文件保存成功 folder.getPath() File.separator newName);// 返回上传文件的访问路径// 例如http://localhost:9999/2022/02/22/df9a66f1-760b-4f95-9faf-b5a216966718.pngString filePath request.getScheme() :// request.getServerName() : request.getServerPort() request.getContextPath() / format newName;return R.ok().put(filePath, filePath);} catch (IOException e) {throw new RRException(系统错误);}}前端
参考spring boot 整合 minio存储 【使用篇】 返回得到一个地址即可访问
问题
已解决 由于该项目涉及token访问链接报错 但我看网络里又有token
已解决
通过访问路径解决token问题 el-uploadclassupload-demorefuploaddragaction#:on-changehandleChangeSelect:on-exceedhandleExceed:file-listfileList:limit1multiple:auto-uploadfalsei classel-icon-upload/idiv classel-upload__text将文件拖到此处或em点击上传/em/divdivclassel-upload__tipslottip只能上传jpg/png文件且不超过500kb/divdiv classel-upload__tip slottip访问路径a :href${filePath}?token${token} target_blank点击跳转{{ filePath }}/a
/div/el-upload创建token变量获取
this.token this.$cookie.get(token)
访问路径404问题
修改yml配置文件 修改前
spring:web:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}修改后
spring:resources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
# 设置Http能访问的本地资源路径
demo:web:upload-path: D:/MineFile/zuoye/xm/equipment-management-system/qhjdata/参考
SpringBoot实现本地文件存储及预览