Java使用FastDFS模块

本文介绍了如何在Java项目中集成FastDFS模块,通过详细的yaml配置和Java代码示例,展示了文件上传的流程,帮助开发者理解并实现FastDFS在Java环境中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

yaml文件配置:

#项目通用配置
lg:
  upload:
    baseUrl: http://image.legou.com/
    filePath: E:\myproject\upload
    allowTypes:
      - image/jpeg
      - image/png
      - image/bmp

#FastDFS
fdfs:
  so-timeout: 2500 #超时时间
  connect-timeout: 600 #连接超时时间
  thumb-image: # 缩略图
    width: 60
    height: 60
  tracker-list: # tracker地址
- 192.168.248.128:22122

Java代码:

package cn.wangkf.service.impl;

import cn.wangkf.config.UploadProperties;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import cn.wangkf.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Objects;

/**
 *
 */
@Service
@EnableConfigurationProperties(UploadProperties.class)
public class UploadServiceImpl implements UploadService {

    @Autowired
    private FastFileStorageClient storageClient;

    private static final Logger logger= LoggerFactory.getLogger(UploadServiceImpl.class);

    @Autowired
    private UploadProperties prop;
    //支持上传的文件类型
    //private static final List<String> suffixes = Arrays.asList("image/png","image/jpeg","image/jpg");

    @Override
    public String upload(MultipartFile file) {
        /**
         * 1.图片信息校验
         *      1)校验文件类型
         *      2)校验图片内容
         * 2.保存图片
         *      1)生成保存目录
         *      2)保存图片
         *      3)拼接图片地址
         */
        try {
            String type = file.getContentType();
            if (!prop.getAllowTypes().contains(type)) {
                logger.info("上传文件失败,文件类型不匹配:{}", type);
                return null;
            }
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null) {
                logger.info("上传失败,文件内容不符合要求");
                return null;
            }
            
            //把文件保存在本地
           /* File dir = new File(prop.getFilePath());
            if (!dir.exists()){
                dir.mkdirs();
            }
            file.transferTo(new File(dir, Objects.requireNonNull(file.getOriginalFilename())));
            String url = prop.getBaseUrl() + file.getOriginalFilename();*/

            //把文件上传到FastDFS
            StorePath storePath = this.storageClient.uploadFile(
                  file.getInputStream(), file.getSize(), StringUtils.substringAfterLast(file.getOriginalFilename(), "."), null);
            String url = prop.getBaseUrl() + storePath.getFullPath();

            return url;
        }catch (Exception e){
            return null;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值