当前位置: 首页 > 后端技术 > Java

文件上传组件-XSpringFileStorage使用

时间:2023-04-01 18:28:07 Java

XSpringFileStorage可以简化上传文件代码,可以配置多个上传平台。需要引入的依赖cn.xuyanwuspring-file-storage0.7.0com.huaweicloudesdk-obs-java3.22.3.1配置文件spring:file-storage:#文件存储配置default-platform:local-1#默认使用的存储平台,thumbnail-suffix:".min.jpg"#缩略图后缀,如【.min.jpg】【.png】local:#本地存储(不推荐),不使用可以省略-platform:local-1#存储平台标识符enable-storage:true#开启存储enable-access:true#开启访问(请在线使用Nginx配置,效率更高)domain:"http://127.0.0.1:9966/test/file/"#访问域名,为example:"http://127.0.0.1:8030/test/file/",注意后面要和path-patterns保持一致,以"/"结尾,本地存储建议使用相对路径(/test/file/),为了方便定期更换域名base-path:E:/Temp/test/#存储地址path-patterns:/test/file/**#访问路径,开启enable-access后,上传的文件huawei-obs可以访问通过这个路径:#华为云OBS,可以不用-platform:huawei-obs-1#存储平台标识enable-storage:true#启用存储access-key:aksecret-key:skend-point:epbucket-name:bndomain:/#访问域名,注意“/”结尾,例如:http://abc.obs.com/base-path:/#basepath注意有个-各个平台前面的标志,通过Multiplelocals可以通过以下方式配置:-platform:local-1#存储平台标识符enable-storage:trueenable-access:truedomain:""base-path:D:/Temp/test/path-patterns:/test/file/**-platform:local-2#存储平台标识,注意这里不能重复enable-storage:trueenable-access:truedomain:""base-path:D:/Temp/test2/path-patterns:/test2/file/**开始类调整@EnableFileStorage@SpringBootApplicationpublicclassSpringTestApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringTestApplication.class,args);}}@RestController的简单使用publicclassFileDetailController{@AutowiredprivateFileStorageServicefileStorageService;//注入真实列/***上传文件,成功返回文件url*/@PostMapping("/upload")publicStringupload(MultipartFilefile){FileInfofileInfo=fileStorageService.of(file).setPath("upload/")//保存到相对路径,为了方便管理,可以不用写。setObjectId("0")//关联对象id,为了方便管理,不需要写.setObjectType("0")//关联对象类型,为了方便管理,不需要写.putAttr("role","admin")//保存一些属性,可以保存在切面和保存上传记录、自定义存储平台等地方获取和使用,不需要写.setPlatform("huawei-obs-1")//使用指定存储平台,如果没有,使用默认存储平台.upload();//上传文件到对应的地方returnfileInfo==null?“上传失败!”:文件信息.getUrl();}}XSpringFileStorage上传不仅支持本地和华为云obs上传,还支持阿里云OSS、腾讯云COS、百度云BOS等更多平台,更多关于X的文件上传、查询、删除方法SpringFileStorage,请参考官网:链接