当前位置:   article > 正文

Spring Boot集成Ceph实现文件上传下载_springboot ceph

springboot ceph

我们搭建一个单独的工程,专门用于实现文件上传和文件下载,子工程坐标如下(父工程就是普通的Spring boot继承父工程)pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>mall-service</artifactId>
  7. <groupId>com.springcloud</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>mall-file-service</artifactId>
  12. <description>
  13. 文件上传和下载
  14. </description>
  15. <dependencies>
  16. <!-- Rados Java Api依赖 -->
  17. <dependency>
  18. <groupId>com.ceph</groupId>
  19. <artifactId>rados</artifactId>
  20. <version>0.6.0</version>
  21. </dependency>
  22. <!-- Cephfs 文件系统依赖 -->
  23. <dependency>
  24. <groupId>com.ceph</groupId>
  25. <artifactId>libcephfs</artifactId>
  26. <version>0.80.5</version>
  27. </dependency>
  28. <!--swift-->
  29. <dependency>
  30. <groupId>org.javaswift</groupId>
  31. <artifactId>joss</artifactId>
  32. <version>0.10.2</version>
  33. </dependency>
  34. </dependencies>
  35. </project>

bootstrap.yml:

  1. server:
  2. port: 8082
  3. spring:
  4. application:
  5. name: mall-file
  6. cloud:
  7. nacos:
  8. config:
  9. file-extension: yaml
  10. server-addr: 192.168.175.101:8848
  11. discovery:
  12. #Nacos的注册地址
  13. server-addr: 192.168.175.101:8848
  14. ceph:
  15. username: cephtester:subtester #Ceph配置 主用户名:子用户名
  16. password: springcloud #秘钥
  17. authUrl: http://192.168.175.101:7480/auth/1.0 #接口访问路径
  18. defaultContainerName: user_datainfo #默认容器名字
  19. #图片路径
  20. cephurl: http://localhost:8082/file/download/
  21. #日志配置
  22. logging:
  23. pattern:
  24. console: "%msg%n"

在类中创建`Account`和`Container`对象,代码如下:

  1. package com.springcloud.mall.file.ceph;
  2. import lombok.Data;
  3. import org.javaswift.joss.client.factory.AccountConfig;
  4. import org.javaswift.joss.client.factory.AccountFactory;
  5. import org.javaswift.joss.client.factory.AuthenticationMethod;
  6. import org.javaswift.joss.model.Account;
  7. import org.javaswift.joss.model.Container;
  8. import org.springframework.boot.context.properties.ConfigurationProperties;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. /**
  12. * <p>内容</p>
  13. * 2022/3/14/0014 14:18
  14. *
  15. * @author lvjie
  16. */
  17. @Configuration
  18. @ConfigurationProperties(prefix = "ceph")
  19. @Data
  20. public class ContainerConfig {
  21. private String username;
  22. private String password;
  23. private String authUrl;
  24. private String defaultContainerName;
  25. /***
  26. * Ceph的账户信息配置
  27. * @return
  28. */
  29. @Bean
  30. public Account account(){
  31. // Ceph的账户信息配置
  32. AccountConfig config = new AccountConfig();
  33. config.setUsername(username);
  34. config.setPassword(password);
  35. config.setAuthUrl(authUrl);
  36. config.setAuthenticationMethod(AuthenticationMethod.BASIC);
  37. return new AccountFactory(config).createAccount();
  38. }
  39. /***
  40. * 容器对象
  41. * @return
  42. */
  43. @Bean
  44. public Container container(){
  45. // 获取容器信息
  46. Container newContainer = account().getContainer(defaultContainerName);
  47. if(!newContainer.exists()) {
  48. return newContainer.create();
  49. }else {
  50. return newContainer;
  51. }
  52. }
  53. }

创建文件上传下载工具类:

  1. package com.springcloud.mall.file.ceph;
  2. import org.javaswift.joss.model.Container;
  3. import org.javaswift.joss.model.StoredObject;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * <p>文件上传下载工具类</p>
  8. * 2022/3/14/0014 14:21
  9. *
  10. * @author lvjie
  11. */
  12. @Component
  13. public class FileHandler {
  14. @Autowired
  15. private Container container;
  16. /****
  17. * 文件上传
  18. */
  19. public void upload(String filename,byte[] buffer) {
  20. //获取容器对象
  21. StoredObject object = container.getObject(filename);
  22. //文件上传
  23. object.uploadObject(buffer);
  24. }
  25. /***
  26. * 文件下载
  27. */
  28. public byte[] download(String filename){
  29. //获取容器中远程存储的信息
  30. StoredObject object = container.getObject(filename);
  31. //执行文件下载
  32. byte[] bytes = object.downloadObject();
  33. return bytes;
  34. }
  35. }

上传下载控制类:

  1. package com.springcloud.mall.file.controller;
  2. import com.springcloud.mall.file.ceph.FileHandler;
  3. import com.springcloud.mall.util.RespResult;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.bind.annotation.*;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import javax.servlet.ServletOutputStream;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. /**
  12. * <p>上传下载控制类</p>
  13. * 2022/3/14/0014 14:23
  14. *
  15. * @author lvjie
  16. */
  17. @RestController
  18. @RequestMapping(value = "/file")
  19. public class FileController {
  20. @Autowired
  21. private FileHandler fileHandler;
  22. @Value("${cephurl}")
  23. private String cephurl;
  24. /***
  25. * 文件上传
  26. * @param file
  27. * @return
  28. */
  29. @PostMapping(value = "/upload")
  30. public RespResult upload(MultipartFile file) throws IOException {
  31. //上传
  32. fileHandler.upload(file.getOriginalFilename(),file.getBytes());
  33. return RespResult.ok(cephurl+file.getOriginalFilename());
  34. }
  35. /***
  36. * 下载
  37. * @return
  38. */
  39. @GetMapping(value = "/download/{filename}")
  40. public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {
  41. //下载
  42. byte[] bytes = fileHandler.download(filename);
  43. //输出文件
  44. ServletOutputStream os = response.getOutputStream();
  45. os.write(bytes);
  46. }
  47. }

启动类:

  1. package com.springcloud.mall.file;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  5. /**
  6. * <p>启动类</p>
  7. * 2022/3/14/0014 14:25
  8. *
  9. * @author lvjie
  10. */
  11. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  12. public class MallFileApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(MallFileApplication.class,args);
  15. }
  16. }

可以打开postman测试上传下载

上传:

http://localhost:8082/file/upload 例如上传一个文件1.jpg

测试下载:

http://localhost:8082/file/download/1.jpg

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/559658
推荐阅读
相关标签
  

闽ICP备14008679号