之前用到文件上传功能,在这里做个学习记录。使用minio实现,后面会记录使用fastdfs和阿里云的oss实现文件上传以及他们的比较(oss根据流量收费)。minio的中文文档:https://docs.min.io/cn/
首先创建一个Springboot项目,在resources中的application.yml文件添加如下配置:
server:
port: 8080
spring:
servlet:
multipart:
enabled: true #开启文件上传
max-file-size: 500MB
max-request-size: 500MB
minio:
endpoint: http://localhost:9000 #Minio服务所在地址
bucketName: tcc #存储桶名称
accessKey: tanchuntcc #访问的key
secretKey: tanchuntcc #访问的秘钥
添加minio的maven依赖:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
controller的代码如下:
@RestController("/minioDemo")
public class MinioDemoController {
private static final Logger LOGGER = LoggerFactory.getLogger(MinioDemoController.class);
@Value("${minio.endpoint}")
private String ENDPOINT;
@Value("${minio.bucketName}")
private String BUCKETNAME;
@Value("${minio.accessKey}")
private String ACCESSKEY;
@Value("${minio.secretKey}")
private String SECRETKEY;
//文件创建
@PostMapping
public String upload(MultipartFile file) {
String s=null;
try {
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
//存入bucket不存在则创建,并设置为只读
if (!minioClient.bucketExists(BUCKETNAME)) {
minioClient.makeBucket(BUCKETNAME);
minioClient.setBucketPolicy(BUCKETNAME, "*.*", PolicyType.READ_ONLY);
}
String filename = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 文件存储的目录结构
String objectName = sdf.format(new Date()) + "/" + filename;
// 存储文件
minioClient.putObject(BUCKETNAME, objectName, file.getInputStream(), file.getContentType());
LOGGER.info("文件上传成功!");
s=ENDPOINT + "/" + BUCKETNAME + "/" + objectName;
} catch (Exception e) {
LOGGER.info("上传发生错误: {}!", e.getMessage());
}
return s;
}
//文件删除
@DeleteMapping
public String delete(String name) {
try {
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
minioClient.removeObject(BUCKETNAME, name);
} catch (Exception e) {
return "删除失败"+e.getMessage();
}
return "删除成功";
}
}
@GetMapping
public void downloadFiles(@RequestParam("filename") String filename, HttpServletResponse httpResponse) {
try {
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
InputStream object = minioClient.getObject(BUCKETNAME, filename);
byte buf[] = new byte[1024];
int length = 0;
httpResponse.reset();
httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
httpResponse.setContentType("application/octet-stream");
httpResponse.setCharacterEncoding("utf-8");
OutputStream outputStream = httpResponse.getOutputStream();
while ((length = object.read(buf)) > 0) {
outputStream.write(buf, 0, length);
}
outputStream.close();
} catch (Exception ex) {
LOGGER.info("导出失败:", ex.getMessage());
}
}
编写好demo程序后使用postman进行测试:使用postman文件上传时选择body中的form-data选项,然后属性栏里面选择file属性就可以实现文件上传了。
删除文件时输入文件的相对路径即可删除:
导出文件时填好文件名称,选择save and download即可在postman中导出文件:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/122097509
内容来源于网络,如有侵权,请联系作者删除!