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

发布时间:2026/7/28 16:24:26
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;}}