一、Spring Boot 中集成 fastdfs文件上传

二、实现fastdfs文件上传与延迟删除功能的Spring Boot项目

1、Spring Boot 版本

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.7</version>`在这里插入代码片`
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

2、 fastdfs 版本依赖

在您的pom.xml文件中,添加FastDFS客户端的依赖项。这里使用的是tobato提供的FastDFS客户端库。

		<dependency>
			<groupId>com.github.tobato</groupId>
			<artifactId>fastdfs-client</artifactId>
			<version>1.26.6</version>
		</dependency>

3、增加配置文件

在application.properties或application.yml中配置FastDFS服务器的地址和其他相关参数。

# fastdfs文件系统 
fdfs:
  tracker-list: #TrackerList参数,支持多个
    - 192.168.162.235:22122
    #- 192.168.162.239:22122
  so-timeout: 1501
  connect-timeout: 601
  thumb-image: #缩略图生成参数
    width: 200
    height: 200
  pool:
    max-total: 500
    max-wait-millis: 5000
    max-total-per-key: 100

4、创建FastDFS配置类,增加工具类

 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

import lombok.extern.slf4j.Slf4j;

/**
 * FastDFS文件上传下载包装类
 */
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@Component
@Slf4j
public class FileClient {

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        log.info("FileClient->uploadFile,file={}", file);
        String extension = getExtension(file);
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                extension, null);
        return getResAccessUrl(storePath);
    }

	public static String getExtension(MultipartFile file) {
		String trimStr = StringUtils.trim(file.getOriginalFilename());
        String extension = FilenameUtils.getExtension(trimStr);
		return extension;
	}

    public String uploadFile(InputStream inputStream, Long fileSize, String originalFilename) throws IOException {
        StorePath storePath = storageClient.uploadFile(inputStream, fileSize,
                FilenameUtils.getExtension(StringUtils.trim(originalFilename)), null);
        return getResAccessUrl(storePath);
    }

    public String uploadFile(File file) throws IOException {
        String extension = FilenameUtils.getExtension(StringUtils.trim(file.getName()));
        //Fastdf扩展名不能为空,默认为"txt"
        if (StringUtils.isEmpty(extension)) {
            extension = "txt";
        }
        StorePath storePath = storageClient.uploadFile(Files.newInputStream(file.toPath()), file.length(),
                extension, null);
        return getResAccessUrl(storePath);
    }


    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content       文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(storePath);
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = "/"+storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 传图片并同时生成一个缩略图 "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadImageAndCrtThumbImage(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getResAccessUrl(storePath);
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件访问地址
     */
    public boolean deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
        	 log.error("删除文件异常,fileUrl is empty");
            return true;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            log.error("删除文件异常:", e);
            return false;
        }
        return true;
    }

   
}

6、 增加对外接口

	@ApiOperation(value = "1、上传单个文件到FastDFS", notes = "")
	@PostMapping("uploadSingle")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "file", value = "单个文件", required = true, dataType = "__file",dataTypeClass =MultipartFile.class ) })
 	public Result<FileItem> uploadSingle(FileUploadVo fileUploadVo) {
		log.info("FileApiController->uploadSingle processing......");
 
		FileItem fileItem = null;
		try {
			fileItem = uploadThenReturn(fileUploadVo.getFile(), fileUploadVo.getProjectCode(), fileUploadVo.getDeleteDelaySeconds());
			return ResultKit.succeed(fileItem);
		} catch (Exception e) {
			String errorInfo = LogKit.format("FileApiController->uploadSingleMap error,fileItem:{}.", fileItem);
			String content = errorInfo + ExceptionKit.getErrorInfoFromException(e);
 
 
			return ResultKit.error(ResultEnums.FAIL.getCode(),e.getMessage());
		}
	}

	private FileItem uploadThenReturn(MultipartFile file, String projectCode, Integer deleteDelaySeconds)
			throws IOException {
		String fileName = file.getOriginalFilename();
		log.info("FileApiController->uploadThenReturn start,fileName:{}.", fileName);
		String url = fileClient.uploadFile(file);
		String extension = FileClient.getExtension(file);
		FileItem fileItem = new FileItem(fileName, url, file.getSize(), extension);
		fileLogBizService.addLog(fileItem, projectCode, deleteDelaySeconds);
		log.info("FileApiController->uploadThenReturn end,fileItem:{}.", fileItem);
		return fileItem;
	}

	@ApiOperation(value = "2、删除文件")
	@PostMapping("delete")
	public Result<Boolean> delete(@RequestParam String projectCode, @RequestParam String token,
			@RequestParam String fileUrl, Integer delaySeconds) {
		log.info("FileApiController->delete start,fileUrl={}", fileUrl);
 
		boolean succeed = fileLogBizService.deleteFile(projectCode, fileUrl, delaySeconds);
		return ResultKit.succeedOrError(succeed);
	}


	@ApiOperation(value = "3、下载文件", notes = "")
	@GetMapping("download")
	public void download(@RequestParam String projectCode, @RequestParam String token, HttpServletResponse response,
			@RequestParam String url) throws IOException {
 
		String saveDir = FileOpenController.getTmpFileDir() + File.separator;
		// linux出现了,目录不会自动创建
		FileUtils.forceMkdir(new File(saveDir));
		try {
			String fdsfUrl = fileBizService.handleUrlDomain(url);
			FileKit.writeUrlFileToOutputStream(fdsfUrl, saveDir, response.getOutputStream());
		} catch (Exception e) {
			log.error("FileApiController->downloadFile error", e);
		}
	}

	public static String getTmpFileDir() {
	    String saveDir = FileKit.getTmpDir() + File.separator + "file";
	    return saveDir;
	}
	
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤来整合 Spring Boot 3.1.1 和 FastDFS: 1. 添加 FastDFS 依赖:在你的 Spring Boot 项目的 pom.xml 文件添加 FastDFS 的依赖: ```xml <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.29.0</version> </dependency> ``` 2. 配置 FastDFS 连接:在 application.properties 或 application.yml 文件添加 FastDFS 的连接配置,包括 tracker 服务器地址、http 服务器地址等信息。例如: ```yaml fdfs: tracker-list: 10.0.0.1:22122,10.0.0.2:22122 http-server: http://10.0.0.1/ ``` 3. 创建 FastDFS 客户端配置类:创建一个 FastDFS 客户端配置类,用于读取上一步配置的连接信息。例如: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class FastDFSConfig { @Value("${fdfs.tracker-list}") private String trackerList; @Value("${fdfs.http-server}") private String httpServer; // Getter methods // ... } ``` 4. 创建 FastDFS 客户端工具类:创建一个 FastDFS 客户端工具类,用于上、下载文件等操作。可以使用 fastdfs-client-java 提供的 API 进行相关操作。 ```java import org.csource.fastdfs.*; public class FastDFSUtil { private static TrackerClient trackerClient; private static TrackerServer trackerServer; private static StorageServer storageServer; private static StorageClient1 storageClient; static { try { ClientGlobal.initByProperties("fastdfs-client.properties"); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } catch (Exception e) { e.printStackTrace(); } } // 实现文件、下载等方法 // ... } ``` 5. 使用 FastDFS 客户端进行文件:在需要上文件的地方,通过 FastDFSUtil 类调用相关方法实现文件。例如: ```java import org.springframework.web.multipart.MultipartFile; public class FileService { public String uploadFile(MultipartFile file) { try { byte[] fileBytes = file.getBytes(); String originalFilename = file.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); String fileId = FastDFSUtil.uploadFile(fileBytes, extName); return fileId; } catch (Exception e) { e.printStackTrace(); } return null; } // 其他业务逻辑 // ... } ``` 这样,你就可以在 Spring Boot 项目整合 FastDFS 并实现文件功能了。请注意,上述代码仅供参考,你可能需要根据你的具体需求进行适当调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值