news 2026/7/28 16:24:11

tracker_server服务器搭建好以后,fastdfs图片上传 具体在项目中的应用(附完整代码,资源文件)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
tracker_server服务器搭建好以后,fastdfs图片上传 具体在项目中的应用(附完整代码,资源文件)

环境一定要先搭建好,搭建的详细步骤和资源文件详见上一篇博客:https://blog.csdn.net/qq_37767455/article/details/100175038

一、运行截图






最后浏览器访问:

二、主要用到的代码

1.配置文件


2.controller


3.service

@OverridepublicAppResponseuploadFile(Map<String,Object>param){AppResponse appResponse=null;try{MultipartFile multipartFile[]=(MultipartFile[])param.get("files");for(inti=0;i<multipartFile.length;i++){FastDFSFile file=newFastDFSFile();//file.setAuthor((String) param.get("userId"));//图片格式后缀String ext=multipartFile[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 commodityFilePath=newCommodityFilePath();//返回图片路径commodityFilePath.setFilePath(serverFdfs+filePath[0]+"/"+filePath[1]);appResponse=AppResponse.success("上传成功",commodityFilePath);}}catch(Exceptione){appResponse=AppResponse.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 logger=LoggerFactory.getLogger(FastDfsUtil.class);static{try{String filePath=newClassPathResource("../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_list=newNameValuePair[1];meta_list[0]=newNameValuePair("author",file.getAuthor());longstartTime=System.currentTimeMillis();String[]uploadResults=null;StorageClient storageClient=null;try{storageClient=getTrackerClient();uploadResults=storageClient.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(uploadResults==null&&storageClient!=null){logger.error("upload file fail, error code:"+storageClient.getErrorCode());}String groupName=uploadResults[0];String remoteFileName=uploadResults[1];logger.info("upload file successfully!!!"+"group_name:"+groupName+", remoteFileName:"+" "+remoteFileName);returnuploadResults;}publicstaticFileInfogetFile(String groupName,String remoteFileName){try{StorageClient storageClient=getTrackerClient();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 storageClient=getTrackerClient();byte[]fileByte=storageClient.download_file(groupName,remoteFileName);InputStream ins=newByteArrayInputStream(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 storageClient=getTrackerClient();byte[]fileByte=storageClient.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 storageClient=getTrackerClient();inti=storageClient.delete_file(groupName,remoteFileName);logger.info("delete file successfully!!!"+i);}publicstaticStorageServer[]getStoreStorages(String groupName)throwsIOException{TrackerClient trackerClient=newTrackerClient();TrackerServer trackerServer=trackerClient.getConnection();returntrackerClient.getStoreStorages(trackerServer,groupName);}publicstaticServerInfo[]getFetchStorages(String groupName,String remoteFileName)throwsIOException{TrackerClient trackerClient=newTrackerClient();TrackerServer trackerServer=trackerClient.getConnection();returntrackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);}publicstaticStringgetTrackerUrl()throwsIOException{return"http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";}privatestaticStorageClientgetTrackerClient()throwsIOException{TrackerServer trackerServer=getTrackerServer();StorageClient storageClient=newStorageClient(trackerServer,null);returnstorageClient;}privatestaticTrackerServergetTrackerServer()throwsIOException{TrackerClient trackerClient=newTrackerClient();TrackerServer trackerServer=trackerClient.getConnection();returntrackerServer;}}

FastDFSFile类:

publicclassFastDFSFile{privateString name;privatebyte[]content;privateString ext;privateString md5;privateString author;publicStringgetName(){returnname;}publicvoidsetName(String name){this.name=name;}publicbyte[]getContent(){returncontent;}publicvoidsetContent(byte[]content){this.content=content;}publicStringgetExt(){returnext;}publicvoidsetExt(String ext){this.ext=ext;}publicStringgetMd5(){returnmd5;}publicvoidsetMd5(String md5){this.md5=md5;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(String author){this.author=author;}}

5.还有用到的类

CommodityFilePath:

publicclassCommodityFilePath{String filePath;publicStringgetFilePath(){returnfilePath;}publicvoidsetFilePath(String filePath){this.filePath=filePath;}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/28 16:23:19

当监管机构开始调取你的模型偏差日志——1份符合NIST AI RMF 1.1标准的公平性文档模板(含21个强制字段)

更多请点击&#xff1a; https://kaifayun.com 第一章&#xff1a;AI 偏见与公平性 AI 系统并非价值中立&#xff0c;其决策逻辑深度嵌入训练数据的分布特征、标注者的主观判断以及算法设计者的隐含假设。当历史数据中存在系统性社会偏见&#xff08;如性别薪酬差距、种族信贷…

作者头像 李华
网站建设 2026/7/28 16:23:18

职场压力监测:汗液生物指标与可穿戴设备的技术解析

1. 项目背景与核心概念 "反KPI生物学"这个听起来有些叛逆的概念&#xff0c;实际上是对现代职场量化管理方式的一种巧妙回应。在绩效指标&#xff08;KPI&#xff09;主导的工作环境中&#xff0c;许多职场人士发现自己的生理状态被各种可穿戴设备持续监测——心率、…

作者头像 李华
网站建设 2026/7/28 16:21:48

卡文断更怎么办?10款高效写小说软件横评(含防踩雷指南)

作为一名天天和文字打交道的同行&#xff0c;我太懂大家卡文时的焦虑了。现在市面上的辅助写作工具层出不穷&#xff0c;但对于我们这些靠码字吃饭的人来说&#xff0c;到底哪些是能真正提升效率的工具&#xff0c;哪些又是白白浪费时间的呢&#xff1f; 前阵子我开新坑遇到严…

作者头像 李华
网站建设 2026/7/28 16:17:59

[实验室环境配置] kaldi安装

下载kaldi 包 git clone https://github.com/kaldi-asr/kaldi.git检查并自动安装依赖库 cd tools ./extras/check_dependencies.sh # all OK 说明成功 make # 这个过程需要消耗一点时间&#xff0c;会下载openfst等工具 # 之所以要在tools文件夹下make的原因&#xff1a;https:…

作者头像 李华
网站建设 2026/7/28 16:16:08

LangChain工具集解析:AI应用开发实战指南

1. LangChain内置工具全景概览作为当下最热门的LLM应用开发框架&#xff0c;LangChain内置的工具集&#xff08;Tools&#xff09;是其区别于其他AI开发平台的核心竞争力。我在实际项目中发现&#xff0c;90%的常见AI应用场景都可以通过合理组合这些内置工具快速实现。不同于需…

作者头像 李华