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/7/28 16:24:30 阅读更多 →
暑假的感受

暑假的感受

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

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

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

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

2026/7/28 16:24:30 阅读更多 →

最新新闻

C++11右值引用与移动语义:高性能编程的核心机制

C++11右值引用与移动语义:高性能编程的核心机制

1. 项目概述&#xff1a;为什么C11的右值引用是性能优化的关键一步如果你写过一段时间的C&#xff0c;尤其是在处理容器、字符串或者自定义资源管理类时&#xff0c;大概率会对“深拷贝”带来的性能开销感到头疼。想象一下&#xff0c;你有一个包含一万个元素的std::vector<…

2026/7/28 16:33:32 阅读更多 →
HFS文件服务器:轻松搭建个人云存储与文件共享平台

HFS文件服务器:轻松搭建个人云存储与文件共享平台

HFS文件服务器&#xff1a;轻松搭建个人云存储与文件共享平台 【免费下载链接】hfs HFS is a web file server for your computer. Share folders or even a single file thanks to the virtual file system. 项目地址: https://gitcode.com/gh_mirrors/hf/hfs 在数字化…

2026/7/28 16:33:32 阅读更多 →
AI 项目落地避坑指南:从 PoC 到生产环境的五个致命鸿沟

AI 项目落地避坑指南:从 PoC 到生产环境的五个致命鸿沟

AI 项目落地避坑指南&#xff1a;从 PoC 到生产环境的五个致命鸿沟 一、"PoC 成功, 生产失败"&#xff1a;AI 项目的独特风险模型 传统软件项目的风险主要在开发阶段——需求理解偏差、技术实现困难、测试覆盖不足。但 AI 项目的风险曲线恰恰相反&#xff1a;PoC 阶段…

2026/7/28 16:33:32 阅读更多 →
物联网硬件安全:SE050安全芯片与PIC24FJ256GA110的实战应用

物联网硬件安全:SE050安全芯片与PIC24FJ256GA110的实战应用

1. 为什么物联网设备需要硬件级安全方案在智能家居和工业物联网项目中&#xff0c;我经常遇到这样的困境&#xff1a;设备固件被逆向、通信数据被窃听、OTA升级包被篡改。传统基于软件的安全方案存在致命缺陷——当攻击者获取设备控制权后&#xff0c;所有加密算法和密钥都暴露…

2026/7/28 16:33:32 阅读更多 →
Go 性能优化避坑总结:pprof 不会告诉你的十个隐性性能杀手

Go 性能优化避坑总结:pprof 不会告诉你的十个隐性性能杀手

Go 性能优化避坑总结&#xff1a;pprof 不会告诉你的十个隐性性能杀手 一、pprof 的"可见性盲区"&#xff1a;火焰图照不到的角落 Go 的性能剖析工具链&#xff08;pprof、trace、benchstat&#xff09;是行业标杆&#xff0c;但工具优势本身产生了认知盲区——开发者…

2026/7/28 16:33:32 阅读更多 →
01 准备你的开发环境

01 准备你的开发环境

准备你的开发环境 1. 下载Anaconda Anaconda 是一个免费开源的Python和R语言的发行版本&#xff0c;简化了包管理和部署&#xff0c;而且支持多个Python环境的隔离。强烈安利&#xff0c;下载地址为Anaconda。推荐下载Python3.7 版本&#xff0c;毕竟Python2 20年久不再维护了…

2026/7/28 16:32:32 阅读更多 →

日新闻

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿&#xff01;3步让你的暗影精灵笔记本重获新生 【免费下载链接】OmenSuperHub Control Omen laptop performance, fan speeds, and keyboard lighting, and unlock power limits. 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 你是否也曾为官方Om…

2026/7/28 0:00:43 阅读更多 →
RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

做 RAG 的人应该都踩过这个致命的坑&#xff1a;把几百页的财报、法规、技术手册扔给向量库&#xff0c;问一个具体问题&#xff0c;搜出来的全是沾边但没用的内容 —— 关键信息要么被硬切块拆碎了&#xff0c;要么藏在几十条结果的最下面。语义相似≠真正相关&#xff0c;这个…

2026/7/28 0:00:43 阅读更多 →
抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

2026年做短视频运营&#xff0c;从抖音上扒文案早就不是偷偷抄笔记的事了。我刚开始做内容的时候&#xff0c;每天刷半小时抖音&#xff0c;手动把爆款视频的口播敲进备忘录&#xff0c;一条2分钟的视频得花十来分钟&#xff0c;碰到语速快的还要反复回听。后来试了一圈工具&am…

2026/7/28 0:00:43 阅读更多 →

周新闻

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 数据集6000张 完整源码已标注数据集训练好的模型环境配置教程程序运行说明文档&#xff0c;可以直接使用&#xff01;系统支持图片、视频、摄像头等多种方式检测裂缝&#xff0c;功能强大实用。 1数据集6000张 8各类别

2026/7/28 12:04:22 阅读更多 →
深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

pubg数据集 精选原图1.42万数据 1.49万标签 无任何重复、算法增强或冗余图像&#xff01; pubg绝地求生目标检测数据集 1分类&#xff1a;e_body&#xff0c;14905个标签&#xff0c;txt格式 共计14244张图&#xff0c;99%为640*640尺寸图像 适合yolo目标检测、AI训练关键词&am…

2026/7/28 8:29:16 阅读更多 →
Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex检测数据集数据集详情检测类别&#xff1a; allies enemy tag图片总量&#xff1a;7247张训练集&#xff1a;5139张验证集&#xff1a;1425张测试集&#xff1a;683张标注状态&#xff1a;全部已标注&#xff0c;即拿即用数据格式&#xff1a;支持YOLO格式及其他格式&#…

2026/7/28 5:03:42 阅读更多 →

月新闻