代码拉取完成,页面将自动刷新
同步操作将从 bluemiaomiao/fastdfs-spring-boot-starter 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
一个同时兼容SpringBoot1.x和2.x的高性能FastDFS客户端.
自动添加依赖
初始化配置项
基于Commons Pool2 实现的高性能连接池
更多操作FastDFS的API
下载.
git clone https://gitee.com/xiaxia_01/fastdfs-spring-boot-starter.git
cd fastdfs-spring-boot-starter
安装到本地仓库.
mvn clean install
mvn source:jar install
mvn javadoc:jar install
添加到项目.
<dependency>
<groupId>com.bluemiaomiao</groupId>
<artifactId>fastdfs-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
在主配置类上添加注解 (@EnableFastdfsClient
).
@EnableFastdfsClient
@SpringBootApplication
public class DemoApplication {
@Autowired
private FastdfsClientService fastdfsClientService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
添加配置条目(application.properties).
fastdfs.nginx-servers=192.168.43.191:80
fastdfs.tracker-servers=192.168.43.191:22122
fastdfs.http-secret-key=FastDFSFrobot@123.
fastdfs.http-anti-steal-token=true
fastdfs.http-tracker-http-port=8080
fastdfs.network-timeout=30
fastdfs.connect-timeout=5
fastdfs.connection-pool-max-idle=18
fastdfs.connection-pool-min-idle=2
fastdfs.connection-pool-max-total=18
fastdfs.charset=UTF-8
添加配置条目(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: 8080
connection-pool-max-idle: 20
connection-pool-max-total: 20
connection-pool-min-idle: 2
nginx-servers: 192.168.43.191:80
tracker-servers: 192.168.43.191:22122
即刻享受它带来的便利.
@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;
}
// 下载文件
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());
}
// 删除文件
String group = "group1";
String storage = "M00/00/00/wKgrv15mOZ2ARgsCAAFBAKhNrcY006.png";
try {
//i = 0 表示删除成功
int i = remoteService.delete(group,storage);
} catch (Exception e) {
e.getMessage();
}
// 当启用防盗链机制时,需要使用该方法下载文件
FastdfsClientService.autoDownloadWithToken(String fileGroup, String remoteFileName, String clientIpAddress)
// 当没有启用防盗链机制时,需要使用该方法下载文件
FastdfsClientService.autoDownloadWithoutToken(String fileGroup, String remoteFileName, String clientIpAddress)
// 下载文件的方法
FastdfsClientService.download(String groupName, String remoteFileName)
// 上传文件的方法
FastdfsClientService.autoUpload(byte[] buffer, String ext)
// 删除文件的方法
FastdfsClientService.delete(String groupName, String remoteFileName)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。