环境一定要先搭建好,搭建的详细步骤和资源文件详见上一篇博客: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;}}