Java校园淘项目实战(三)————Springboot+fastDFS实现文件上传

上一章节,我们讲了如何在CentOS 8 系统中安装好fastDFS,这一章节,我们就来写上传图片的功能,并且拿到自动生成的缩略图。
(1)在pom.xml中引入相关依赖,我这里的版本是1.26.2

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

(2)在yml文件里面配置一下fdfs,指定生成的缩略图为150x150px,其中的tracker-list是踪迹服务器的地址,这个要根据自己的虚拟机地址来写

fdfs:
  connect-timeout: 1500
  so-timeout: 1000
  tracker-list:
    - 192.168.228.132:22122
  thumb-image:
    height: 150
    width: 150

(3)写配置类,让其自动配置在IOC容器中

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)  // 解决jmx重复注册bean的问题
public class FdfsConfig {
}

(4)接下来写上传功能

/**
 * 文件上传工具类
 */
@Component
public class FastDfsUtils {

    private static String host = "192.168.228.132";

    private static String port = "80";

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 缩略图
     */
    @Autowired
    private ThumbImageConfig thumbImageConfig;

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) {
        //忽略不是图片的内容
        if (file == null) {
            return null;
        }
        String contentType = file.getContentType();
        List<String> list = Arrays.asList("image/jpeg", "image/png", "image/jpg");
        if (!list.contains(contentType)) {
            return "图片格式错误";
        }

        BufferedImage read = null;
        try {
            read = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (read == null) {
            return "图片内容错误";
        }

        //过滤成功后,开始上传文件
        StorePath storePath = null;
        try {
            storePath = storageClient.uploadImageAndCrtThumbImage((InputStream) file.getInputStream(),
                    file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String group = storePath.getGroup();
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());

        // http://192.168.228.132:80/xxxx
        return "http://" + host + ":" + port + "/" + group + "/" + path;
    }
}

这里需要注意一下,必须要得到group才行,因为通过thumbImageConfig.getThumbImagePath(storePath.getPath());得到的路径是没有携带group的,所以需要在storePath里面获取gourp,然后拼接,这样才是完整的路径,在浏览器中访问的时候,不会出错。

(5)在Controller里面写接口

 @PostMapping("upload")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        if (file == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        String s = fastDfsUtils.uploadFile(file);
        if (s == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        return ResponseEntity.ok(s);
    }

好了,现在就可以用postman测试一下,经过我的测试,是能够访问的。不知道怎么测试的,自行百度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值