SpringBoot项目中Fastdfs配置及使用2

相关链接:FastDFS安装教程1(安装、配置、部署、防盗链、nginx、图片压缩)

1、下载

git clone https://gitee.com/xiaxia_01/fastdfs-spring-boot-starter.git
cd fastdfs-spring-boot-starter

2、安装到本地仓库

mvn clean install
mvn source:jar install
mvn javadoc:jar install

如果直接在maven仓库相应位置复制该项目生成的jar包,即可忽略以上两步.
3、添加到项目


    com.bluemiaomiao
    fastdfs-spring-boot-starter
    1.0-SNAPSHOT

4、在主配置类上添加注解 (@EnableFastdfsClient)

@EnableFastdfsClient
@SpringBootApplication
public class DemoApplication {
    @Autowired
    private FastdfsClientService fastdfsClientService;
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

5、添加配置条目(application.yml)

fastdfs:
  charset: UTF-8
  connect-timeout: 5
  http-secret-key: FastDFSFrobot@123.
  network-timeout: 30
  http-anti-steal-token: true
  http-tracker-http-port: 8888
  connection-pool-max-idle: 20
  connection-pool-max-total: 20
  connection-pool-min-idle: 2
  nginx-servers: 10.18.25.163:8888
  tracker-servers: 10.18.25.163:22122

6、完成以上配置,即可在项目里直接调用FastdfsClientService使用,示例如下

@Autowired
private FastdfsClientService remoteService;

// 上传文件
String[] remoteInfo;
try {
    remoteInfo = remoteService.autoUpload(image.getBytes(), type);
    log.info("上传的服务器分组: " + remoteInfo[0]);
    log.info("上传的服务器ID: " + remoteInfo[1]);
    log.info("上传的服务器分组和ID: " + remoteInfo[2]);
} catch (Exception e) {
    log.error("Upload file error: " + e.getMessage());
    return HttpStatus.INTERNAL_SERVER_ERROR;
}

例:


image.png
// 下载文件1
String groupName = "group1";
String remoteFileName = "M00/00/00/wKgrv15nCj2APtknAAFBAKhNrcY954.png";
String remoteFile = "Get file error.";
try {
//当启用防盗链机制时,需要使用该方法下载文件
remoteFile1 = fastdfs.autoDownloadWithToken(groupName, remoteFileName, remoteAddress);
// 当没有启用防盗链机制时,需要使用该方法下载文件
remoteFile2 = fastdfs.autoDownloadWithoutToken(groupName, remoteFileName, remoteAddress);
} catch (Exception e) {
   log.error("Get file error: " + e.getMessage());
}

例:

remoteFile1= http://192.168.43.191:80/group1/M00/00/00/wKgrv15onbqAQ5u3AAFBAKhNrcY705.png?token=7445c459a8a9e5c7962debf7ce1b1af5&ts=1583914426
remoteFile1= http://192.168.43.191:80/group1/M00/00/00/wKgrv15onbqAQ5u3AAFBAKhNrcY705.png
// 下载文件2
String groupName = "group1";
String remoteFileName = "M00/00/00/wKgrv15nCj2APtknAAFBAKhNrcY954.png";
String localFileName = "yy.png";
try {
    byte[] bitys = remoteService.download(groupName, remoteFileName);
    InputStream ins = new ByteArrayInputStream(bitys);
    String contentType = request.getServletContext().getMimeType(localFileName);
    String contentDisposition = "attachment;filename=" + localFileName;
    // 设置头
    response.setHeader("Content-Type", contentType);
    response.setHeader("Content-Disposition", contentDisposition);
    // 获取绑定了客户端的流
    ServletOutputStream output = response.getOutputStream();
    // 把输入流中的数据写入到输出流中
    IOUtils.copy(ins, output);
    ins.close();
    output.close();
} catch (Exception e) {
    log.error("Get file error: " + e.getMessage());
}

例:

说明:一般文件服务器是不对外开放的,所以这里展示了通过流方式将图片传给前端场景,避免因直接访问导致图片请求失败问题
image.png
// 删除文件
String group = "group1";
String storage = "M00/00/00/wKgrv15mOZ2ARgsCAAFBAKhNrcY006.png";
try {  
  //i = 0 表示删除成功
  int i = remoteService.delete(group,storage);  
} catch (Exception e) {
  e.getMessage();
}

例:


image.png

文章参考:
https://gitee.com/bluemiaomiao/fastdfs-spring-boot-starter

你可能感兴趣的:(SpringBoot项目中Fastdfs配置及使用2)