分布式文件存储-FastDFS

1.pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>	web依赖
        </dependency>
        <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>		分布式文件系统依赖
            <version>1.27.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.changgou</groupId>
            <artifactId>changgou_common</artifactId>	公共资源依赖
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

​ 2.fdfs_client.conf

connect_timeout = 60				连接超时时间60秒
network_timeout = 60				通信超时时间60秒
charset = UTF-8						字符编码utf-8
http.tracker_http_port = 8080		设置tracker端口8080
tracker_server = 192.168.200.128:22122		服务连接地址

​ 3.application.yml

spring:					
  servlet:
    multipart:
      max-file-size: 10MB				上传文件大小最大10MB
      max-request-size: 10MB			请求文件大小最大10MB
server:
  port: 9008
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true

​ 4.FileApplication.java

@SpringBootApplication
@EnableEurekaClient
public class FileApplication {
    public static void main(String[] args) {
        SpringApplication.run(FileApplication.class,args);
    }
}
拓展内容:fastDFS工具类底层原理分析
  //初始化配置文件
	ClientGlobal.init(filePath)
  //创建TrackerClient 
	TrackerClient trackerClient = new TrackerClient(); 
  //获取TrackerServer 
	TrackerServer trackerServer = trackerClient.getConnection();
  //创建StorageServer 
	StorageServer = null;
  //创建StorageClient 
	StorageClient storageClient = new StorageClient(trackerServer, storageServer);

	storageClient.upload_file       (file.getContent(), file.getExt(), meta_list) 	上传文件:String[]
	storageClient.get_file_info   	(groupName, remoteFileName)		查询文件:FileInfo
	storageClient.download_file 	(groupName, remoteFileName)		下载文件:InputStream
	storageClient.delete_file       (groupName, remoteFileName)		删除文件:void

​ 5.FileController.java

@RestController
@RequestMapping("/file")
public class FileController {
    @PostMapping("/upload")					文件上传必须为post请求
    public Result uploadFile(MultipartFile file){			文件类型必须是:MultipartFile
        try{
            //判断文件是否存在
            if (file == null){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的扩展名称  abc.jpg   jpg
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //获取文件内容
            byte[] content = file.getBytes();
            //创建文件上传的封装实体类
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,content,extName);
            //基于工具类进行文件上传,并接受返回参数  String[]
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);
            //封装返回结果
            String url = FastDFSClient.getTrackerUrl()+uploadResult[0]+"/"+uploadResult[1];
            return new Result(true,StatusCode.OK,"文件上传成功",url);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR,"文件上传失败");
    }
}

​ 6. Postman测试文件上传

发布了55 篇原创文章 · 获赞 4 · 访问量 9846

猜你喜欢

转载自blog.csdn.net/weixin_45678915/article/details/104364613
今日推荐