一、创建一个maven的web project,叫file-manager:
mvn archetype:create -DgroupId=platform.activity.filemanager -DartifactId=file-manager -DarchetypeArtifactId=maven-archetype-webapp
二、定义一个fastDFS的客户端文件fdfs_client.conf:
connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.1.156:22122 #tracker_server = 192.168.1.188:22122 #storage_server = 192.168.1.155:23000 #no need here
三、定义一个配置接口:
/** * */ package com.chuanliu.platform.activity.fm.manager; import java.io.Serializable; /** * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public interface FileManagerConfig extends Serializable { public static final String FILE_DEFAULT_WIDTH = "120"; public static final String FILE_DEFAULT_HEIGHT = "120"; public static final String FILE_DEFAULT_AUTHOR = "Diandi"; public static final String PROTOCOL = "http://"; public static final String SEPARATOR = "/"; public static final String TRACKER_NGNIX_PORT = "8080"; public static final String CLIENT_CONFIG_FILE = "fdfs_client.conf"; }
四、封装一个FastDFS文件Bean
/** * */ package com.chuanliu.platform.activity.fm.manager; /** * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public class FastDFSFile implements FileManagerConfig { private static final long serialVersionUID = -996760121932438618L; private String name; private byte[] content; private String ext; private String height = FILE_DEFAULT_HEIGHT; private String width = FILE_DEFAULT_WIDTH; private String author = FILE_DEFAULT_AUTHOR; public FastDFSFile(String name, byte[] content, String ext, String height, String width, String author) { super(); this.name = name; this.content = content; this.ext = ext; this.height = height; this.width = width; this.author = author; } public FastDFSFile(String name, byte[] content, String ext) { super(); this.name = name; this.content = content; this.ext = ext; } public byte[] getContent() { return content; } public void setContent(byte[] content) { this.content = content; } public String getExt() { return ext; } public void setExt(String ext) { this.ext = ext; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
五、定义核心的FileManager类,里面包含有上传、删除、获取文件的方法:
/** * */ package com.chuanliu.platform.activity.fm.manager; import java.io.File; import java.io.IOException; import org.apache.log4j.Logger; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.FileInfo; import org.csource.fastdfs.ServerInfo; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import com.chuanliu.platform.activity.basic.util.LoggerUtils; /** * File Manager used to provide the services to upload / download / delete the files * from FastDFS. * * <note>In this version, FileManager only support single tracker, will enhance this later...</note> * * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public class FileManager implements FileManagerConfig { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(FileManager.class); private static TrackerClient trackerClient; private static TrackerServer trackerServer; private static StorageServer storageServer; private static StorageClient storageClient; static { // Initialize Fast DFS Client configurations try { String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath(); String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE; logger.info("Fast DFS configuration file path:" + fdfsClientConfigFilePath); ClientGlobal.init(fdfsClientConfigFilePath); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageClient = new StorageClient(trackerServer, storageServer); } catch (Exception e) { LoggerUtils.error(logger, e); } } public static String upload(FastDFSFile file) { LoggerUtils.info(logger, "File Name: " + file.getName() + " File Length: " + file.getContent().length); NameValuePair[] meta_list = new NameValuePair[3]; meta_list[0] = new NameValuePair("width", "120"); meta_list[1] = new NameValuePair("heigth", "120"); meta_list[2] = new NameValuePair("author", "Diandi"); long startTime = System.currentTimeMillis(); String[] uploadResults = null; try { uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list); } catch (IOException e) { logger.error("IO Exception when uploadind the file: " + file.getName(), e); } catch (Exception e) { logger.error("Non IO Exception when uploadind the file: " + file.getName(), e); } logger.info("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); if (uploadResults == null) { LoggerUtils.error(logger, "upload file fail, error code: " + storageClient.getErrorCode()); } String groupName = uploadResults[0]; String remoteFileName = uploadResults[1]; String fileAbsolutePath = PROTOCOL + trackerServer.getInetSocketAddress().getHostName() + SEPARATOR + TRACKER_NGNIX_PORT + SEPARATOR + groupName + SEPARATOR + remoteFileName; LoggerUtils.info(logger, "upload file successfully!!! " +"group_name: " + groupName + ", remoteFileName:" + " " + remoteFileName); return fileAbsolutePath; } public static FileInfo getFile(String groupName, String remoteFileName) { try { return storageClient.get_file_info(groupName, remoteFileName); } catch (IOException e) { logger.error("IO Exception: Get File from Fast DFS failed", e); } catch (Exception e) { logger.error("Non IO Exception: Get File from Fast DFS failed", e); } return null; } public static void deleteFile(String groupName, String remoteFileName) throws Exception { storageClient.delete_file(groupName, remoteFileName); } public static StorageServer[] getStoreStorages(String groupName) throws IOException { return trackerClient.getStoreStorages(trackerServer, groupName); } public static ServerInfo[] getFetchStorages(String groupName, String remoteFileName) throws IOException { return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName); } }
六、Unit Test测试类
package manager; /** * */ import java.io.File; import java.io.FileInputStream; import org.csource.fastdfs.FileInfo; import org.csource.fastdfs.ServerInfo; import org.csource.fastdfs.StorageServer; import org.junit.Test; import org.springframework.util.Assert; import com.chuanliu.platform.activity.fm.manager.FastDFSFile; import com.chuanliu.platform.activity.fm.manager.FileManager; /** * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public class TestFileManager { @Test public void upload() throws Exception { File content = new File("C:\\520.jpg"); FileInputStream fis = new FileInputStream(content); byte[] file_buff = null; if (fis != null) { int len = fis.available(); file_buff = new byte[len]; fis.read(file_buff); } FastDFSFile file = new FastDFSFile("520", file_buff, "jpg"); String fileAbsolutePath = FileManager.upload(file); System.out.println(fileAbsolutePath); fis.close(); } @Test public void getFile() throws Exception { FileInfo file = FileManager.getFile("group1", "M00/00/00/wKgBm1N1-CiANRLmAABygPyzdlw073.jpg"); Assert.notNull(file); String sourceIpAddr = file.getSourceIpAddr(); long size = file.getFileSize(); System.out.println("ip:" + sourceIpAddr + ",size:" + size); } @Test public void getStorageServer() throws Exception { StorageServer[] ss = FileManager.getStoreStorages("group1"); Assert.notNull(ss); for (int k = 0; k < ss.length; k++){ System.err.println(k + 1 + ". " + ss[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + ss[k].getInetSocketAddress().getPort()); } } @Test public void getFetchStorages() throws Exception { ServerInfo[] servers = FileManager.getFetchStorages("group1", "M00/00/00/wKgBm1N1-CiANRLmAABygPyzdlw073.jpg"); Assert.notNull(servers); for (int k = 0; k < servers.length; k++) { System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort()); } } }
相关代码见附件的zip包。
使用方法:
将zip包解压后,直接在eclipse import existing maven projec。
相关推荐
8. **Spring Boot应用实例**:提到的“全套示例框架源码”可能包含了以上所有技术的实战案例,这些案例可以帮助开发者更好地理解如何在实际项目中应用这些技术,提升技能水平。 这些技术的组合使用,可以构建出一个...
这个压缩包中的视频教程将通过实例演示上述所有知识点,帮助你从零开始快速掌握Dubbo及其周边技术。通过观看和实践,你将具备解决实际开发中遇到问题的能力,为构建高可用、高性能的分布式系统打下坚实的基础。无论...
以下是常见的C++笔试面试题及其核心知识点解析,帮助您系统复习
计算机短期培训教案.pdf
计算机二级Access笔试题库.pdf
下是一份关于C++毕业答辩的心得总结,内容涵盖技术准备、答辩技巧和注意事项,供参考
内容概要:本文档详细介绍了英特尔为苹果公司构建的基于智能处理单元(IPU)的Cassandra集群的技术验证(PoC)。主要内容涵盖IPU存储用例、已建存储PoC、MEV到MMG400的过渡、苹果构建IPU-Cassandra集群的动机以及PoC开发进展。文档还探讨了硬件配置、软件环境设置、性能调优措施及其成果,特别是针对延迟和吞吐量的优化。此外,文档展示了六节点Cassandra集群的具体架构和测试结果,强调了成本和复杂性的降低。 适合人群:对分布式数据库系统、NoSQL数据库、IPU技术感兴趣的IT专业人员和技术管理人员。 使用场景及目标:适用于希望了解如何利用IPU提升Cassandra集群性能的企业技术人员。主要目标是展示如何通过IPU减少服务器部署的成本和功耗,同时提高数据处理效率。 其他说明:文档中涉及的内容属于机密级别,仅供特定授权人员查阅。文中提到的技术细节和测试结果对于评估IPU在大规模数据中心的应用潜力至关重要。
计算机二级考试C语言题.pdf
计算机发展史.pdf
计算机仿真技术系统的分析方法.pdf
yolo编程相关资源,python编程与YOLO算法组成的坐姿检测系统,功能介绍: 一:实时检测学生错误坐姿人数 二:通过前端阿里云平台显示上传数据,实现数据可视化
办公室网安全监控uptime-kuma,docker镜像离线压缩包
计算机课程设计-网络编程项目源码.zip
将该dll包放入项目并引用,可以操作打印机
杰奇2.3内核淡绿唯美小说网站源码 PC+手机版 自动采集 全站伪静态,送10.1版本关关采集器
计算机辅助教学.pdf
内容概要:本文详细介绍了如何利用天文相机和其他相关硬件设备搭建一套高画质、高帧率的流星监控系统,以及针对红色精灵闪电这一特殊自然现象的捕捉方法。文中不仅涵盖了硬件的选择标准如CMOS靶面尺寸、量子效率等重要参数,还提供了基于Python和OpenCV实现的基本监控代码示例,包括亮度突变检测、运动检测算法等关键技术点。此外,对于安装位置的选择、供电方式、成本控制等方面也有具体的指导建议。 适用人群:对天文摄影感兴趣的爱好者,尤其是希望捕捉流星和红色精灵闪电等瞬时天文现象的专业人士或业余玩家。 使用场景及目标:适用于希望搭建个人天文观测站,用于科学研究或个人兴趣爱好的场景。目标是能够稳定可靠地捕捉到流星和红色精灵闪电等难以捉摸的天文现象,为研究提供高质量的数据资料。 其他说明:文中提到的一些技术和方法虽然较为复杂,但对于有一定编程基础和技术动手能力的人来说是非常实用的参考资料。同时,文中提供的省钱技巧也为预算有限的用户提供了一些有价值的建议。