携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第15天,点击查看活动详情
每日英语:
What you are you do not see,what you see is your shadow.
你看不见你自己,你所看见的只是你的影子。 ——泰戈尔
1.文件服务搭建(承接上篇)
1.1在mall-service工程下面创建一个新的子服务mall-file-service
专门用于实现文件上传和文件下载,工作坐标如下:
<groupId>com.xz</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>mall-file-service</artifactId>
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.xz</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>
1.2在mall-file-service下的resources下面创建bootstrap.yml,配置具体如下:
server:
port: 8082
spring:
application:
name: mall-file
cloud:
nacos:
config:
file-extension: yaml
server-addr: 127.0.0.1:8848
discovery:
#Nacos的注册地址
server-addr: 127.0.0.1:8848
ceph:
username: cephtester:subtester #Ceph配置 主用户名:子用户名
password: xz #秘钥
authUrl: http://192.168.19.131:7480/auth/1.0 #接口访问路径
defaultContainerName: user_datainfo #默认容器名字
#图片路径
cephurl: http://localhost:8082/file/download/
#日志配置
logging:
pattern:
console: "%msg%n"
1.3创建com.xz.mall.file.ContainerConfig配置类,在类中创建Account和Container对象,代码如下:
@Data
@Configuration
@ConfigurationProperties(prefix = "ceph")
public class ContainerConfig {
private String username;
private String password;
private String authUrl;
private String defaultContainerName;
/**
* 1、创建账号
*/
@Bean
public Account account() {
AccountConfig accountConfig = new AccountConfig();
accountConfig.setUsername(username);
accountConfig.setPassword(password);
accountConfig.setAuthUrl(authUrl);
//给一下基础权限
accountConfig.setAuthenticationMethod(AuthenticationMethod.BASIC);
return new AccountFactory(accountConfig).createAccount();
}
/**
* 2、创建容器
*/
@Bean
public Container container() {
Container container = account().getContainer(defaultContainerName);
if (!container.exists()) {
return container.create();
}
return container;
}
}
1.4创建文件上传下载工具类com.xz.mall.file.ceph.FileHandler,两个方法,一个是文件上传,另一个是文件下载,代码如下:
@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;
}
}
1.5控制器创建:com.xz.mall.file.controller.FileController,主要是文件上传和文件下载功能。代码如下:
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FileHandler fileHandler;
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("/upload")
public RespResult upload(MultipartFile file) {
//调用文件上传
try {
fileHandler.upload(file.getOriginalFilename(), file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return RespResult.ok();
}
/**
* 文件下载
* @param filename
* @return
*/
@GetMapping("/download/{filename}")
public void download(@PathVariable String filename, HttpServletResponse servletResponse) throws IOException {
//调用文件下载
byte[] buffer = fileHandler.download(filename);
//文件下载输出到web端
ServletOutputStream os = servletResponse.getOutputStream();
os.write(buffer);
os.flush();
os.close();
}
}
1.6最后创建启动类com.xz.mall.file.MallFileApplication,运行代码进行测试
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MallFileApplication {
public static void main(String[] args) {
SpringApplication.run(MallFileApplication.class,args);
}
}
文件上传测试http://localhost:8082/file/upload
文件下载测试http://localhost:8082/file/download/20220810-test1.png
总结:本篇主要介绍了一下文件服务搭建的具体过程,实现了文件上传和文件下载功能。