美文网首页
FastDFS (二)集成 Spring Boot

FastDFS (二)集成 Spring Boot

作者: _大叔_ | 来源:发表于2020-12-10 18:48 被阅读0次

    引入依赖

            <dependency>
                <groupId>com.github.tobato</groupId>
                <artifactId>fastdfs-client</artifactId>
                <version>1.26.7</version>
            </dependency>
    

    添加配置

    fdfs:
      # 连接的超时时间
      connect-timeout: 3000
      # 读取的超时时间
      so-timeout: 3000
      #tracker服务所在的ip地址和端口号
      tracker-list: 10.240.3x.xx2:22122
    

    Controller

    @RestController
    @RequestMapping("/img")
    public class ImgController {
    
        @Autowired
        private ImgServer imgServer;
    
        @PostMapping("/push")
        public ResponseData push(@RequestParam("file") MultipartFile file){
            return imgServer.push(file);
        }
    
    }
    

    实现

    @Service
    @Slf4j
    public class ImgServerImpl implements ImgServer {
        @Autowired
        FastFileStorageClient fastFileStorageClient;
    
        @Override
        public ResponseData<String> push(MultipartFile file) {
            if (file.isEmpty()) {
                return ResponseData.failureResponse(UploadFileCode.UPLOAD_FILE_CODE_1000);
            }
            try {
                log.info("开始上传 {}", file.getOriginalFilename());
                String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
                StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),fileSuffix, null);
                String path = storePath.getFullPath();
                log.info("上传成功");
                return ResponseData.successResponse(path);
            } catch (IOException e) {
                log.error(e.toString(), e);
                return ResponseData.failureResponse(UploadFileCode.UPLOAD_FILE_CODE_1001.getCode(), e.toString());
            }
        }
    }
    

    测试

    相关文章

      网友评论

          本文标题:FastDFS (二)集成 Spring Boot

          本文链接:https://www.haomeiwen.com/subject/fyprgktx.html