# easyFileUpload **Repository Path**: fearwall/easyFileUpload ## Basic Information - **Project Name**: easyFileUpload - **Description**: 集成了fastDFS, 阿里云OSS, seaweedFS的客户端上传及删除功能组件,可以自由选择的切换文件系统,并统一了三种文件系统的返回值格式 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2022-05-03 - **Last Updated**: 2022-05-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 文件上传组件使用 ## 问题 每次开发都会出现选择文件服务器的问题, 本组件解决java后台文件上传的问题,集成了fastdfs,seaweedfs,oss三款服务器的上传及删除功能具体操作方式如下 ## 操作步骤 ### 1 添加pom依赖 ```xml com.yvan easyupload 0.0.2-RELEASE thirdparty Nexus Repository http://10.12.102.197:10002/nexus/content/repositories/thirdparty/ ``` ### 2 上传指定配置文件到项目的`src/main/resources`文件夹下 项目需要哪个就创建哪个 aliyunoss_client.properties ```properties #>>>>>>>>>>>>>>>>>>>>>阿里云OSS配置<<<<<<<<<<<<<<<<<<<<<<< #可以选择其他的地址 oss.endpoint=http://oss-cn-hangzhou.aliyuncs.com #已经在控制台创建的bucket oss.bucketName=chunan #你上传文件的保存路径,如果bucket中不存在则创建(其实原理并不是文件夹,只是文件名,详情请先阅读官方文档) oss.picLocation=chunan/picFile/ oss.accessKeyId= oss.accessKeySecret= ``` fdfs_client.properties ```properties #默认值为30s connect_timeout=10 #默认值为30s network_timeout=30 charset=UTF-8 http.tracker_http_port=80 # token 防盗链功能 http.anti_steal_token=false http.secret_key=FastDFS1234567890 tracker_server=10.10.77.62:22122 ``` seaweedFS_client.properties ```properties #文件上传IP端口 seaweedFS.writeHost=10.12.102.197 seaweedFS.writePort=9333 seaweedFS.timeout=15000 #文件访问IP端口 seaweedFS.readHost=10.12.102.197 seaweedFS.readPort=9333 ``` ### 3 修改`application.properties`文件 在文件中增加(必填) ```properties #>>>>>>>>>>>>文件上传(fastdfs,seaweedfs,oss)<<<<<<<<<<<<<<< file.uploader=oss #配置文件位置,为空时,文件位置在resources根目录 #--如果使用spring.profiles.active变量,需要resources根目录下创建config目录,并指定文件位置: #eg: file.settings.activeDir=config/${spring.profiles.active} file.settings.activeDir= ``` ### 4 创建controller接口 接口地址自定义 ```java package com.citycloud.dum.sanitation.controller; import com.citycloud.dum.sanitation.constant.ResponseMsg; import com.yvan.upload.FileOperationUtils; import com.yvan.upload.FileUploadResultVo; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; @RestController public class DFSController { //当使用spring.profiles.active变量控制文件系统配置文件参数 @Value("${file.settings.activeDir}") private String activeDir; @PostMapping("/upload") public ResponseMsg singleFileUpload(@RequestParam("file") MultipartFile file) throws IOException { String filename = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); //使用spring.profiles.active变量控制文件系统配置文件参数 FileOperationUtils.activeDir = activeDir; FileUploadResultVo resultVo = FileOperationUtils.upload(filename, inputStream); return new ResponseMsg<>(resultVo); } @PostMapping("/delete") public ResponseMsg singleFileDelete(String fileUrl) { boolean delete = FileOperationUtils.delete(fileUrl); return new ResponseMsg<>(delete,delete ? "删除成功" : "删除失败"); } } ``` ## 各文件服务器上传返回值 ### 1 OSS ```json { "totalCount": null, "data": { "fileName": "FBEF2C53-7257-4EE5-9B28-2BCC831B52F5.png", "fileExt": "png", "fileSize": 67280, "fileId": null, "groupName": null, "fileUrl": "http://chunan.oss-cn-hangzhou.aliyuncs.com/chunan/picFile/171D427880DC4B5B8DA1C3C7EDB1E5E6.png" }, "success": true, "code": null, "message": "操作成功", "lastTime": "2020-07-02 11:01:28", "url": null } ``` ### 2 seaweedFS ```json { "totalCount": null, "data": { "fileName": "FBEF2C53-7257-4EE5-9B28-2BCC831B52F5.png", "fileExt": "png", "fileSize": 67280, "fileId": "15,032de8b50676", "groupName": null, "fileUrl": "http://10.12.102.197:9333/15,032de8b50676" }, "success": true, "code": null, "message": "操作成功", "lastTime": "2020-07-02 10:43:26", "url": null } ``` ### 3 fastDFS ```json { "totalCount": null, "data": { "fileName": "M00/03/8F/CgpNPl79TdCAAUhcAAEG0PX1Mfk303.png", "fileExt": "png", "fileSize": 67280, "fileId": null, "groupName": "group1", "fileUrl": "http://10.10.77.62:80/group1/M00/03/8F/CgpNPl79TdCAAUhcAAEG0PX1Mfk303.png" }, "success": true, "code": null, "message": "操作成功", "lastTime": "2020-07-02 11:00:32", "url": null } ``` ## 各文件服务器删除统一返回值 ### 1 成功 ```json { "totalCount": null, "data": null, "success": true, "code": null, "message": "删除成功", "lastTime": "2020-07-02 10:44:48", "url": null } ``` ### 2 失败 ```json { "totalCount": null, "data": null, "success": false, "code": null, "message": "删除失败", "lastTime": "2020-07-02 10:44:48", "url": null } ```