先将之前nginx 配置里 暂时的图片服务器配置删除:

 

用 FastDFS分 布式文件系统 改造leyou-upload工程。

1.引入依赖
(因为在父工程中,我们已经管理了依赖版本,所以这里只需加入坐标即可)

 

2.引入配置类

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

 

3.编写FastDFS属性

 

4.配置hosts

 

5.测试

添加测试类:

@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Test
    public void testUpload() throws FileNotFoundException {
        // 要上传的文件
        File file = new File("D:\\T\\123.jpg");
        // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
        StorePath storePath = this.storageClient.uploadFile(
                new FileInputStream(file), file.length(), "jpg", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
    }

    @Test
    public void testUploadAndCreateThumb() throws FileNotFoundException {
        File file = new File("D:\\T\\123.jpg");
        // 上传并且生成缩略图
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                new FileInputStream(file), file.length(), "png", null);
        // 带分组的路径
        System.out.println(storePath.getFullPath());
        // 不带分组的路径
        System.out.println(storePath.getPath());
        // 获取缩略图路径
        String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
        System.out.println(path);
    }
}

 

运行第一个测试方法:
(记得将虚拟机或云服务器防火墙关闭)

 

运行第二个测试方法:

 

访问group1/M00/00/00/rBNYql3mO5WAHLGsAABB1thl5FA213.png 看看(前面要加http://image.leyou.com/
http://image.leyou.com/group1/M00/00/00/rBNYql3mO5WAHLGsAABB1thl5FA213.png

 

然后访问 M00/00/00/rBNYql3mO5WAHLGsAABB1thl5FA213_60x60.png 看看,(前面要加http://image.leyou.com/group1/
http://image.leyou.com/group1/M00/00/00/rBNYql3mO5WAHLGsAABB1thl5FA213_60x60.png

 

所以图片上传成功了,缩略图也生成上传成功了!

 

既然测试成功了,那下面我们再来改造一下上传逻辑,使我们的后台页面也可以使用使用FastDFS上传图片。

@Service
public class UploadService {

    private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif");

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);

    @Autowired
    private DefaultFastFileStorageClient storageClient;

    public String uploadImage(MultipartFile file) {

        String originalFilename = file.getOriginalFilename();
        // 校验文件的类型
        String contentType = file.getContentType();
        if (!CONTENT_TYPES.contains(contentType)){
            // 文件类型不合法,直接返回null
            LOGGER.info("文件类型不合法:{}", originalFilename);
            return null;
        }

        try {
            // 校验文件的内容
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage == null){
                LOGGER.info("文件内容不合法:{}", originalFilename);
                return null;
            }

            // 保存到服务器
            //file.transferTo(new File("D:\\leyou\\images\\" + originalFilename));
            String ext = StringUtils.substringAfterLast(originalFilename, ".");
            StorePath storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null);

            // 生成url地址,返回
           // return "http://image.leyou.com/" + originalFilename;
            return "http://image.leyou.com/" + storePath.getFullPath();
        } catch (IOException e) {
            LOGGER.info("服务器内部错误:{}", originalFilename);
            e.printStackTrace();
        }
        return null;
    }
}

重启upload服务,然后去后台页面试一试:

 

成功了!

我们使用FastDFS客户端改造文件上传就成功了!

 

PS
这里说个坑:

阿里云服务器,关闭了防火墙,还是没有用。

因为阿里云里有个安全组策略,我们需要去配置。

1. 进入阿里云控制台,点击 左边 的 网络与安全 , 然后点击 安全组



 

2. 点击你 安装了 FastDFS 的那台服务器 右侧的  配置规则

 

3.点击  克隆,或者点击  添加安全组规则 (我点击的是克隆,因为点击添加好像配置不了全部端口打开)

 

4. 按如下配置:

 

5. 配置完成了,会自动刷新,也可以自己点下刷新:

 

这样就可以正常访问阿里云服务器上的服务了。

 

有遇到问题的,可以留言讨论啊~

猿兄与你一起学习,一起成长!