【FastDFS】FastDFS引入

news/2025/1/12 0:56:17/

目录

一、pom配置

二、配置fastdfs-client.properties

三、创建FastDFS工具类【实现文件上传、下载、删除、查询】

参考资料


一、pom配置

    	<!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java --><dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.29</version></dependency>
		<!--fastdfs-client-java包引入远程仓库--><repository><id>fit2cloud</id><url>https://repository.fit2cloud.com/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository>

二、配置fastdfs-client.properties

## fastdfs-client.propertiesfastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 10fastdfs.charset = UTF-8fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key =
fastdfs.http_tracker_http_port = 8888# 本地环境Tracker
#fastdfs.tracker_servers = 127.0.0.1:22122## Whether to open the connection pool, if not, create a new connection every time
fastdfs.connection_pool.enabled = true## max_count_per_entry: max connection count per host:port , 0 is not limit
fastdfs.connection_pool.max_count_per_entry = 500## connections whose the idle time exceeds this time will be closed, unit: second, default value is 3600
fastdfs.connection_pool.max_idle_time = 3600## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000

三、创建FastDFS工具类【实现文件上传、下载、删除、查询】

/*** FastDFS工具类【实现文件上传、下载、删除、查询】** @author luomao1*/
public class FastDFSClient {private TrackerClient trackerClient = null;private TrackerServer trackerServer = null;private StorageServer storageServer = null;private StorageClient1 storageClient = null;public FastDFSClient() {try {ClientGlobal.initByProperties(CommonConstant.FAST_DFS_CONF);trackerClient = new TrackerClient();trackerServer = trackerClient.getTrackerServer();storageServer = null;storageClient = new StorageClient1(trackerServer, storageServer);} catch (Exception e) {ApiLogger.error("[FastDFSClient] init fastdfs failed, err=", e);}}public FastDFSClient(String conf) throws Exception {if (conf.contains("classpath:")) {String path = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(), "UTF-8");path = path.substring(6);conf = conf.replace("classpath:", URLDecoder.decode(path, "UTF-8"));}ClientGlobal.init(conf);trackerClient = new TrackerClient();trackerServer = trackerClient.getTrackerServer();storageServer = null;storageClient = new StorageClient1(trackerServer, storageServer);}/*** 上传文件方法* <p>Title: uploadFile</p>* <p>Description: </p>** @param fileName 文件全路径* @param extName  文件扩展名,不包含(.)* @param metas    文件扩展信息* @return* @throws Exception*/public String uploadFile(String fileName, String extName, NameValuePair[] metas) {String result = null;try {result = storageClient.upload_file1(fileName, extName, metas);} catch (IOException e) {e.printStackTrace();} catch (MyException e) {e.printStackTrace();}return result;}/*** 上传文件,传fileName** @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg* @return null为失败*/public String uploadFile(String fileName) {return uploadFile(fileName, null, null);}/*** @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg* @param extName  文件的扩展名 如 txt jpg等* @return null为失败*/public String uploadFile(String fileName, String extName) {return uploadFile(fileName, extName, null);}/*** 上传文件方法* <p>Title: uploadFile</p>* <p>Description: </p>** @param fileContent 文件的内容,字节数组* @param extName     文件扩展名* @param metas       文件扩展信息* @return* @throws Exception*/public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {String result = null;try {result = storageClient.upload_file1(fileContent, extName, metas);} catch (IOException e) {e.printStackTrace();} catch (MyException e) {e.printStackTrace();}return result;}/*** 上传文件** @param fileContent 文件的字节数组* @return null为失败* @throws Exception*/public String uploadFile(byte[] fileContent) throws Exception {return uploadFile(fileContent, null, null);}/*** 上传文件** @param fileContent 文件的字节数组* @param extName     文件的扩展名 如 txt  jpg png 等* @return null为失败*/public String uploadFile(byte[] fileContent, String extName) {return uploadFile(fileContent, extName, null);}/*** 文件下载到磁盘** @param path   图片路径* @param output 输出流 中包含要输出到磁盘的路径* @return -1失败,0成功*/public int download_file(String path, BufferedOutputStream output) {int result = -1;try {byte[] b = storageClient.download_file1(path);try {if (b != null) {output.write(b);result = 0;}} catch (Exception e) {} //用户可能取消了下载finally {if (output != null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}}} catch (Exception e) {e.printStackTrace();}return result;}/*** 获取文件数组** @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg* @return*/public byte[] download_bytes(String path) {byte[] b = null;try {b = storageClient.download_file1(path);} catch (IOException e) {ApiLogger.error("[download_bytes] download bytes IOException error=", e);} catch (MyException e) {ApiLogger.error("[download_bytes] download bytes MyException error=", e);}return b;}/*** 删除文件** @param group       组名 如:group1* @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg* @return -1失败,0成功*/public Integer delete_file(String group, String storagePath) {int result = -1;try {result = storageClient.delete_file(group, storagePath);} catch (IOException e) {e.printStackTrace();} catch (MyException e) {e.printStackTrace();}return result;}/*** @param fileId 文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg* @return -1失败,0成功*/public Integer delete_file(String fileId) {int result = -1;try {result = storageClient.delete_file1(fileId);} catch (IOException e) {ApiLogger.error("[delete_file] IOException failed", e);} catch (MyException e) {ApiLogger.error("[delete_file] MyException failed", e);}return result;}}

参考资料

https://mp.csdn.net/mp_blog/creation/success/126467563


http://www.ppmy.cn/news/616696.html

相关文章

大众点评网地点抓取程序php

<?phpset_time_limit(0);class snap_dianping{//抓取的分类,(*)代表页码static $url array(川菜>http://www.dianping.com/search/category/2/10/g317p(*)/g10g317,北京菜 > http://www.dianping.com/search/category/2/10/g311p(*)/g10g311,湘菜 > http://www.d…

JS+MySQL获取 京东 省市区 地区

采集了一下JD的省市区地区 &#xff08;非常简单&#xff0c;只是做个记录&#xff09; 1.建表&#xff1a;account_area 2.进入页面&#xff1a; https://reg.jd.com/reg/company 在浏览器&#xff08;Firefox&#xff09;控制台&#xff08;Firebug&#xff09;上输入&#x…

一些关于船舶撞桥的数据

根据各种有关资料文献的介绍, 船撞桥事故在世界各地一直在不断地发生, 船撞桥事故的频率远比我们想象的更经常。由船撞桥事故所导致的人员伤亡、财产损失以及环境破坏是惊人的。很多船撞桥事故轻则损失数万元, 重则人员伤亡、损失以数百万、数千万甚至数十亿美元计, 大量的间接…

道路简标的制作方法

道路简标&#xff0c;如国道、省道之类&#xff0c;用一个背景符号&#xff0c;显示数字简称&#xff0c;例如G325.如下图 这个标注的做法需要使用到文本标注里面的背景设置。在【图层属性】-【标注】里面&#xff0c;点击【符号】进行设置 进入【编辑符号】&#xff0c;…

Promise 深度学习

文章目录 Promise 由来Promise的用法reject的用法finally all的用法race的用法总结 Promise 由来 我们处理异步函数最普通的方法是这样的&#xff0c;等待上一次请求结束再执行下一步操作&#xff1a; // 一般以定时器来模拟一次请求 setTimeout(() > {console.log("…

在pycharm上导出Anaconda3的环境配置文件

目录 1.原理&#xff1a; ​2.亲身实践&#xff1a; 1.原理&#xff1a; 要在PyCharm中导出Anaconda3环境的配置文件&#xff0c;可以使用conda命令行工具来完成。请按照以下步骤进行操作&#xff1a; 打开PyCharm&#xff0c;并确保项目使用的是Anaconda3环境。 在PyCha…

.NET 靠开源再“出圈”!

【编者按】从闭源走向开源&#xff0c;.NET 背后都发生了哪些有趣的故事。本文采访了 6 位微软 .NET 团队成员&#xff0c;分享他们在 GitHub 以及建立 .NET 开源项目的经历。 作者 | Richard Lander 译者 | 弯月 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#x…

.NET 5.0来喽

转载&#xff1a;https://blog.csdn.net/powertoolsteam/article/details/109614740?biz_id102&utm_termnet5.0&utm_mediumdistribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-0-109614740&spm1018.2118.3001.4450 我们很高兴今天.NET5.0正式…