分布式存储FastDFS

亦碎流年 2020-05-01

  • 加入依赖
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
  • yml中配置
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
  • fdfs_clients.conf
connect_timeout = 60network_timeout = 60charset = UTF-8http.tracker_http_port = 8080tracker_server = 192.168.200.128:22122
  • 测试文件
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,"文件上传失败");}

相关推荐