tracker_server服务器搭建好以后,fastdfs图片上传 具体在项目中的应用(附完整代码,资源文件)
环境一定要先搭建好搭建的详细步骤和资源文件详见上一篇博客https://blog.csdn.net/qq_37767455/article/details/100175038一、运行截图最后浏览器访问二、主要用到的代码1.配置文件2.controller3.serviceOverridepublicAppResponseuploadFile(MapString,Objectparam){AppResponse appResponsenull;try{MultipartFile multipartFile[](MultipartFile[])param.get(files);for(inti0;imultipartFile.length;i){FastDFSFile filenewFastDFSFile();//file.setAuthor((String) param.get(userId));//图片格式后缀String extmultipartFile[i].getOriginalFilename().substring(multipartFile[i].getOriginalFilename().lastIndexOf(.)1);file.setContent(multipartFile[i].getBytes());file.setName(multipartFile[i].getOriginalFilename());file.setExt(ext);//上传图片String filePath[]FastDfsUtil.upload(file);CommodityFilePath commodityFilePathnewCommodityFilePath();//返回图片路径commodityFilePath.setFilePath(serverFdfsfilePath[0]/filePath[1]);appResponseAppResponse.success(上传成功,commodityFilePath);}}catch(Exceptione){appResponseAppResponse.bizError(上传失败e);}returnappResponse;}4.图片上传 fastdfs工具类importorg.csource.common.NameValuePair;importorg.csource.fastdfs.*;importorg.slf4j.LoggerFactory;importorg.springframework.core.io.ClassPathResource;importjava.io.ByteArrayInputStream;importjava.io.IOException;importjava.io.InputStream;publicclassFastDfsUtil{privatestaticorg.slf4j.Logger loggerLoggerFactory.getLogger(FastDfsUtil.class);static{try{String filePathnewClassPathResource(../resources/fdfs_client.conf).getFile().getAbsolutePath();ClientGlobal.init(../resources/fdfs_client.conf);}catch(Exceptione){logger.error(FastDFS Client Init Fail!,e);}}publicstaticString[]upload(FastDFSFile file){logger.info(File Name: file.getName()File Length:file.getContent().length);NameValuePair[]meta_listnewNameValuePair[1];meta_list[0]newNameValuePair(author,file.getAuthor());longstartTimeSystem.currentTimeMillis();String[]uploadResultsnull;StorageClient storageClientnull;try{storageClientgetTrackerClient();uploadResultsstorageClient.upload_file(file.getContent(),file.getExt(),meta_list);}catch(IOExceptione){logger.error(IO Exception when uploadind the file:file.getName(),e);}catch(Exceptione){logger.error(Non IO Exception when uploadind the file:file.getName(),e);}logger.info(upload_file time used:(System.currentTimeMillis()-startTime) ms);if(uploadResultsnullstorageClient!null){logger.error(upload file fail, error code:storageClient.getErrorCode());}String groupNameuploadResults[0];String remoteFileNameuploadResults[1];logger.info(upload file successfully!!!group_name:groupName, remoteFileName: remoteFileName);returnuploadResults;}publicstaticFileInfogetFile(String groupName,String remoteFileName){try{StorageClient storageClientgetTrackerClient();returnstorageClient.get_file_info(groupName,remoteFileName);}catch(IOExceptione){logger.error(IO Exception: Get File from Fast DFS failed,e);}catch(Exceptione){logger.error(Non IO Exception: Get File from Fast DFS failed,e);}returnnull;}publicstaticInputStreamdownFile(String groupName,String remoteFileName){try{StorageClient storageClientgetTrackerClient();byte[]fileBytestorageClient.download_file(groupName,remoteFileName);InputStream insnewByteArrayInputStream(fileByte);returnins;}catch(IOExceptione){logger.error(IO Exception: Get File from Fast DFS failed,e);}catch(Exceptione){logger.error(Non IO Exception: Get File from Fast DFS failed,e);}returnnull;}publicstaticbyte[]downByte(String groupName,String remoteFileName){try{StorageClient storageClientgetTrackerClient();byte[]fileBytestorageClient.download_file(groupName,remoteFileName);returnfileByte;}catch(IOExceptione){logger.error(IO Exception: Get File from Fast DFS failed,e);}catch(Exceptione){logger.error(Non IO Exception: Get File from Fast DFS failed,e);}returnnull;}publicstaticvoiddeleteFile(String groupName,String remoteFileName)throwsException{StorageClient storageClientgetTrackerClient();intistorageClient.delete_file(groupName,remoteFileName);logger.info(delete file successfully!!!i);}publicstaticStorageServer[]getStoreStorages(String groupName)throwsIOException{TrackerClient trackerClientnewTrackerClient();TrackerServer trackerServertrackerClient.getConnection();returntrackerClient.getStoreStorages(trackerServer,groupName);}publicstaticServerInfo[]getFetchStorages(String groupName,String remoteFileName)throwsIOException{TrackerClient trackerClientnewTrackerClient();TrackerServer trackerServertrackerClient.getConnection();returntrackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);}publicstaticStringgetTrackerUrl()throwsIOException{returnhttp://getTrackerServer().getInetSocketAddress().getHostString():ClientGlobal.getG_tracker_http_port()/;}privatestaticStorageClientgetTrackerClient()throwsIOException{TrackerServer trackerServergetTrackerServer();StorageClient storageClientnewStorageClient(trackerServer,null);returnstorageClient;}privatestaticTrackerServergetTrackerServer()throwsIOException{TrackerClient trackerClientnewTrackerClient();TrackerServer trackerServertrackerClient.getConnection();returntrackerServer;}}FastDFSFile类publicclassFastDFSFile{privateString name;privatebyte[]content;privateString ext;privateString md5;privateString author;publicStringgetName(){returnname;}publicvoidsetName(String name){this.namename;}publicbyte[]getContent(){returncontent;}publicvoidsetContent(byte[]content){this.contentcontent;}publicStringgetExt(){returnext;}publicvoidsetExt(String ext){this.extext;}publicStringgetMd5(){returnmd5;}publicvoidsetMd5(String md5){this.md5md5;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(String author){this.authorauthor;}}5.还有用到的类CommodityFilePathpublicclassCommodityFilePath{String filePath;publicStringgetFilePath(){returnfilePath;}publicvoidsetFilePath(String filePath){this.filePathfilePath;}}

相关新闻

【紧急预警】AI副业时间衰减曲线已启动:第7天起效率断崖式下跌?3步动态重校准协议立即生效

【紧急预警】AI副业时间衰减曲线已启动:第7天起效率断崖式下跌?3步动态重校准协议立即生效

更多请点击: https://codechina.net 第一章:AI副业时间衰减曲线的本质认知与紧急预警信号识别 AI副业的时间投入产出比并非线性增长,而呈现典型的指数级衰减特征——初期高响应、快反馈的“蜜月期”往往掩盖了底层不可持续性。其本质源于三…

2026/11/2 8:49:51 阅读更多 →
暑假的感受

暑假的感受

今天是九月一号,又迎来了一年的 开学季现在处于大二的我们,感觉时间过的好快啊!经过一个月的学 习,感觉自己的暑假过的很充实,学到了很多的东西,认识了很多的朋友。 同时,在这里我认识了自己的师…

2026/11/2 10:15:52 阅读更多 →
为什么你的AI工作流总卡在“能用但不稳”阶段?——拆解17个高隐蔽性断点与6套已验证修复模板

为什么你的AI工作流总卡在“能用但不稳”阶段?——拆解17个高隐蔽性断点与6套已验证修复模板

更多请点击: https://intelliparadigm.com 第一章:为什么你的AI工作流总卡在“能用但不稳”阶段?——拆解17个高隐蔽性断点与6套已验证修复模板 AI工作流在POC阶段常表现良好,却在生产环境频繁出现延迟抖动、输出漂移、上下文截断…

2026/10/31 14:33:16 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/4 1:35:24 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/3 10:57:27 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/3 11:35:49 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/4 6:23:50 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/3 10:47:27 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/3 10:57:33 阅读更多 →

日新闻

周新闻

月新闻