springBoot项目配置fastDFS达到上传、下载资源的教程有很多,也非常方便直接用maven引jar包就可以。针对SpringMVC项目的教程基本上都要单独下载jar包,因为maven远程仓库上没有,在github上找到了我要的jar包。
下载jar包:https://download.csdn.net/download/CharmaineXia/87130424
废话不多说,直接放代码。
fdfs_client.properties:
groupName=group11 #组名 tracker_ip=d-fastdfs.dmsd.tech #服务器地址名 connect_timeout = 30 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.60.59:22122 #地址名+端口号
applicationContext.xml:
<!--加载fastDFS配置文件--> <context:property-placeholder location="classpath:fdfs_client.properties"/>
上传文件:
public class FileUploadServiceImpl implements FileUploadService { // 获取配置文件中的配置IP地址 private String realIp=CONFIGUREIP_STRING; // 获取配置文件中的配置分组 private String group=CONFIGUREIP_STRING; private StorageClient1 storageClient1 = null; /** * @description:初始化仓库客户端对象 * @author: xiashiman * @date: 2022-11-01 16:48 **/ private void initStorageClient1(){ try { String classPath = new File(FileUploadServiceImpl.class.getResource("/").getFile()).getCanonicalPath(); log.info("classPath的值是“”“”“”"+classPath); String confFileName = classPath +File.separator+ "fdfs_client.properties"; //获取配置文件路径 log.info("=== 配置文件的路径是:" + confFileName); ClientGlobal.init(confFileName); //初始化全局客户端? TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group); //实例化追踪器客户端 TrackerServer trackerServer = trackerClient.getConnection(); //连接fastdfs服务器 if (trackerServer == null) { log.error("getConnection fastDFS return null"); } StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); //连接成功,找到合适的存储器 if (storageServer == null) { log.error("getStoreStorage fastDFS return null"); } storageClient1 = new StorageClient1(trackerServer, storageServer); //实例化存储器客户端 } catch (Exception e) { log.error("连接fastDFS服务器发生了异常",e); } } /** * @description:上传文件 * @author: xiashiman * @date: 2022-11-01 14:34 * @param: [fis, fileName] * @return:文件网络链接 **/ private String uploadFile(InputStream fis, String fileName) { try { // 文件信息校验 if (null==fis||fileName.isEmpty()) { log.debug("需要上传的文件信息不通过"); return null; } NameValuePair[] meta_list = null; // new NameValuePair[0]; byte[] file_buff = null; if (fis != null) { int len = fis.available(); file_buff = new byte[len]; fis.read(file_buff); } String fileid = storageClient1.upload_file1(group,file_buff, getFileExt(fileName), meta_list); log.info("文件上传成功,fileid是:" + fileid); // 获取附件的完整地址 String Path = "http"+":"+ "//"+ realIp +"/"+fileid; log.info("文件上传成功,文件地址:" + Path); return Path; } catch (Exception ex) { log.error("上传文件到fastDFS异常:",ex); return null; }finally{ if (fis != null){ try { fis.close(); //关闭链接 } catch (IOException e) { log.error("输入流关闭异常:"+e); } } } } /** * 文件上传 * @param file MultipartFile类型 * @return url */ @Override public String fileUpload(MultipartFile file) throws Exception { try { this.initStorageClient1();//初始化仓库客户端 return this.uploadFile(file.getInputStream(),file.getOriginalFilename()); //调用上传文件方法 } catch (Exception e) { log.error("文件上传发生了异常",e); } throw new Exception(); } }
其他文件操作:
/** * @description:根据组名和远程文件名来删除一个文件 * @author: xiashiman * @date: 2022-11-01 14:40 * @param: [groupName, fileName]fileName:例如"M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg" * @return: 0为成功,非0为失败,具体为错误代码 **/ private int deleteFile(String groupName, String fileName) { try { int result = storageClient1.delete_file(group, fileName); return result; } catch (Exception ex) { log.error("删除fastDFS上fileName为 "+fileName+" 的资源文件失败:",ex); return ZERO_INT; } } /** * @description:根据fileId来删除一个文件 * @author: xiashiman * @date: 2022-11-01 14:41 * @param: [fileId]例如 group1/M00/00/00/ooYBAFM6MpmAHM91AAAEgdpiRC0012.xml * @return: 0为成功,非0为失败,具体为错误代码 **/ private int deleteFile(String fileId) { try { int result = storageClient1.delete_file1(fileId); return result; } catch (Exception ex) { log.error("删除fastDFS上fileId为 "+fileId+" 的资源文件失败:",ex); return ZERO_INT; } } /** * @description:修改一个已经存在的文件 * @author: xiashiman * @date: 2022-11-01 14:43 * @param: [oldFileId, file, filePath] * @param oldFileId:原来旧文件的fileId * @param file:新文件 * @param filePath:新文件路径 * @return: 返回空则为失败 **/ private String modifyFile(String oldFileId, InputStream file, String filePath) { String fileid = null; try { // 先上传 fileid = uploadFile(file, filePath); if (fileid == null) { return null; } // 再删除 int delResult = deleteFile(oldFileId); if (delResult != ZERO_INT) { return null; } } catch (Exception ex) { log.error("修改文件fileId为 "+oldFileId+" 异常:",ex); return null; } return fileid; } /** * @description:文件下载 * @author: xiashiman * @date: 2022-11-01 14:47 * @param: [fileId] * @return: 返回一个流 **/ private InputStream downloadFile(String fileId) { try { byte[] bytes = storageClient1.download_file1(fileId); InputStream inputStream = new ByteArrayInputStream(bytes); return inputStream; } catch (Exception ex) { log.error("下载文件fileId为"+fileId+"的失败:",ex); return null; } } /** * @description:获取文件后缀名(不带点). * @author: xiashiman * @date: 2022-11-01 14:49 * @param: [fileName] * @return: 如:"jpg" or "". **/ private String getFileExt(String fileName) { if (StringUtils.isBlank(fileName) || !fileName.contains(".")) { return ""; } else { return fileName.substring(fileName.lastIndexOf(".") + ONE_INT); // 不带最后的点 } }