有了这个开源工具后,我五点就下班了( 四 )


@PostMApping("/ftp/upload")public void upload() {try {FtpUploadParam param = new FtpUploadParam();param.setHostname(ftpConfig.getServerHostname());param.setPort(ftpConfig.getServerPort());param.setUsername(ftpConfig.getServerUsername());param.setPassword(ftpConfig.getServerPassword());param.setWorkingPath(ftpConfig.getServerWorkingPath());param.setSaveName("xxx.mp3");InputStream in = new FileInputStream(new File("D:/uploadfile/like.mp3"));param.setInputStream(in);ftpUtils.upload(param);} catch (Exception e) {log.error("TestFtpServerController upload 错误:{}", e);}}@PostMapping("/ftp/download")public void download() {try {FtpDownloadParam param = new FtpDownloadParam();param.setHostname(ftpConfig.getServerHostname());param.setPort(ftpConfig.getServerPort());param.setUsername(ftpConfig.getServerUsername());param.setPassword(ftpConfig.getServerPassword());param.setWorkingPath(ftpConfig.getServerWorkingPath());param.setDownloadPath("D:/downloadFile/");param.setFileName("xxx.mp3");ftpUtils.download(param, "xxxx.mp3");} catch (Exception e) {log.error("TestFtpServerController download 错误:{}", e);}}三、分布式文件系统工具  「非结构化数据通常是使用文件的方式进行存储,这时候不可避免地要使用到文件系统进行管理 。」 分布式文件系统工具使用了第三方开源框架seaweedfs进行搭建,可以实现程序上传,删除、下载、查询,并有文件分布式存储,避免单点故障,节约成本等特点 。
  前面也专门通过一篇文章讲解了:为何要使用seaweedfs框架搭建分布式文件系统的,感兴趣的读者可以通过下方链接进行查看:Gitee图床崩溃后,我使用Seaweedfs搭建了文件系统并封装成轮子开源
  「部分源码如下:」
/*** @description: 上传单个文件到文件服务器* @param: file* @return: 文件的fid + 文件的请全访问地址* @author: it*/public String uploadFile(MultipartFile file) throws Exception {FileSource fileSource = getFileSource();FileTemplate fileTemplate = new FileTemplate(fileSource.getConnection());// 上传文件FileHandleStatus handleStatus = fileTemplate.saveFileByStream(file.getOriginalFilename(), file.getInputStream(), contentType);// 获取上传文件的访问地址String fileUrl = fileTemplate.getFileUrl(handleStatus.getFileId());// 关闭当前连接fileSource.shutdown();return handleStatus.getFileId() + StrUtil.DASHED + fileUrl;}/*** @description: 根据文件下载文件* @param: fid* @param: response* @param: fileName* @author: it*/public void downloadFileByFid(HttpServletResponse response, HttpServletRequest request, String fid, String fileName) throws Exception {FileSource fileSource = getFileSource();FileTemplate fileTemplate = new FileTemplate(fileSource.getConnection());StreamResponse fileStream = fileTemplate.getFileStream(fid);// 设置响应头response.setContentType(CommonConstant.CONTENT_TYPE);response.setCharacterEncoding(CommonConstant.UTF_8);String encodeFileName = buildingFileNameAdapterBrowser(request, fileName);response.setHeader(CommonConstant.CONTENT_DISPOSITION, CommonConstant.ATTACHMENT_FILENAME + encodeFileName);// 读取并写入到响应输出InputStream inputStream = fileStream.getInputStream();byte[] fileByte = new byte[inputStream.available()];inputStream.read(fileByte);response.getOutputStream().write(fileByte);response.getOutputStream().flush();fileSource.shutdown();}  「具体使用案例如下:」
/*** @description: 上传文件* @param:* @param: file* @return:* @author: it* @date: 2022/7/14 17:01*/@ResponseBody@RequestMapping("upload")public void uploadFile(MultipartFile file) {try {String fileUrl = seaweedFsUtil.uploadFile(file);System.out.println(fileUrl);} catch (Exception e) {log.error("TestSeaweedFsController uploadFile in error:{}", e);}}/*** @description: 下载文件* @param:* @param: fileId* @return:* @author: it* @date: 2022/7/14 17:01*/@RequestMapping("download")public void downloadFile(HttpServletResponse response, HttpServletRequest request, String fileId, String fileName) {try {seaweedFsUtil.downloadFileByFid(response, request, fileId, fileName);} catch (Exception e) {log.error("TestSeaweedFsController downloadFile in error:{}", e);}}工具更新  到此为止,轮子之王已集成的工具就介绍完毕了,后续还会不断更新、集成新的轮子,下面给大家介绍一下下一段时间项目的一些工作(「如果读者有想要集成的轮子,欢迎提issue或者这文章下面留言」):