赞
踩
我们搭建一个单独的工程,专门用于实现文件上传和文件下载,子工程坐标如下(父工程就是普通的Spring boot继承父工程)pom.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>mall-service</artifactId>
- <groupId>com.springcloud</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>mall-file-service</artifactId>
- <description>
- 文件上传和下载
- </description>
- <dependencies>
- <!-- Rados Java Api依赖 -->
- <dependency>
- <groupId>com.ceph</groupId>
- <artifactId>rados</artifactId>
- <version>0.6.0</version>
- </dependency>
- <!-- Cephfs 文件系统依赖 -->
- <dependency>
- <groupId>com.ceph</groupId>
- <artifactId>libcephfs</artifactId>
- <version>0.80.5</version>
- </dependency>
-
- <!--swift-->
- <dependency>
- <groupId>org.javaswift</groupId>
- <artifactId>joss</artifactId>
- <version>0.10.2</version>
- </dependency>
- </dependencies>
- </project>
bootstrap.yml:
- server:
- port: 8082
- spring:
- application:
- name: mall-file
- cloud:
- nacos:
- config:
- file-extension: yaml
- server-addr: 192.168.175.101:8848
- discovery:
- #Nacos的注册地址
- server-addr: 192.168.175.101:8848
-
- ceph:
- username: cephtester:subtester #Ceph配置 主用户名:子用户名
- password: springcloud #秘钥
- authUrl: http://192.168.175.101:7480/auth/1.0 #接口访问路径
- defaultContainerName: user_datainfo #默认容器名字
- #图片路径
- cephurl: http://localhost:8082/file/download/
-
- #日志配置
- logging:
- pattern:
- console: "%msg%n"
在类中创建`Account`和`Container`对象,代码如下:
- package com.springcloud.mall.file.ceph;
-
- import lombok.Data;
- import org.javaswift.joss.client.factory.AccountConfig;
- import org.javaswift.joss.client.factory.AccountFactory;
- import org.javaswift.joss.client.factory.AuthenticationMethod;
- import org.javaswift.joss.model.Account;
- import org.javaswift.joss.model.Container;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * <p>内容</p>
- * 2022/3/14/0014 14:18
- *
- * @author lvjie
- */
- @Configuration
- @ConfigurationProperties(prefix = "ceph")
- @Data
- public class ContainerConfig {
-
- private String username;
- private String password;
- private String authUrl;
- private String defaultContainerName;
-
- /***
- * Ceph的账户信息配置
- * @return
- */
- @Bean
- public Account account(){
- // Ceph的账户信息配置
- AccountConfig config = new AccountConfig();
- config.setUsername(username);
- config.setPassword(password);
- config.setAuthUrl(authUrl);
- config.setAuthenticationMethod(AuthenticationMethod.BASIC);
- return new AccountFactory(config).createAccount();
- }
-
- /***
- * 容器对象
- * @return
- */
- @Bean
- public Container container(){
- // 获取容器信息
- Container newContainer = account().getContainer(defaultContainerName);
- if(!newContainer.exists()) {
- return newContainer.create();
- }else {
- return newContainer;
- }
- }
- }
创建文件上传下载工具类:
- package com.springcloud.mall.file.ceph;
-
- import org.javaswift.joss.model.Container;
- import org.javaswift.joss.model.StoredObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- /**
- * <p>文件上传下载工具类</p>
- * 2022/3/14/0014 14:21
- *
- * @author lvjie
- */
- @Component
- public class FileHandler {
-
- @Autowired
- private Container container;
-
- /****
- * 文件上传
- */
- public void upload(String filename,byte[] buffer) {
- //获取容器对象
- StoredObject object = container.getObject(filename);
- //文件上传
- object.uploadObject(buffer);
- }
-
- /***
- * 文件下载
- */
- public byte[] download(String filename){
- //获取容器中远程存储的信息
- StoredObject object = container.getObject(filename);
- //执行文件下载
- byte[] bytes = object.downloadObject();
- return bytes;
- }
- }
上传下载控制类:
- package com.springcloud.mall.file.controller;
-
- import com.springcloud.mall.file.ceph.FileHandler;
- import com.springcloud.mall.util.RespResult;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
-
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- /**
- * <p>上传下载控制类</p>
- * 2022/3/14/0014 14:23
- *
- * @author lvjie
- */
- @RestController
- @RequestMapping(value = "/file")
- public class FileController {
- @Autowired
- private FileHandler fileHandler;
-
- @Value("${cephurl}")
- private String cephurl;
- /***
- * 文件上传
- * @param file
- * @return
- */
- @PostMapping(value = "/upload")
- public RespResult upload(MultipartFile file) throws IOException {
- //上传
- fileHandler.upload(file.getOriginalFilename(),file.getBytes());
- return RespResult.ok(cephurl+file.getOriginalFilename());
- }
-
- /***
- * 下载
- * @return
- */
- @GetMapping(value = "/download/{filename}")
- public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {
- //下载
- byte[] bytes = fileHandler.download(filename);
- //输出文件
- ServletOutputStream os = response.getOutputStream();
- os.write(bytes);
- }
- }
启动类:
- package com.springcloud.mall.file;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
-
- /**
- * <p>启动类</p>
- * 2022/3/14/0014 14:25
- *
- * @author lvjie
- */
- @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
- public class MallFileApplication {
- public static void main(String[] args) {
- SpringApplication.run(MallFileApplication.class,args);
- }
- }
可以打开postman测试上传下载
上传:
http://localhost:8082/file/upload 例如上传一个文件1.jpg
测试下载:
http://localhost:8082/file/download/1.jpg
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。