spring-file-storage 文件上传
spring-file-storage 文件上传
·
<!-- spring-file-storage 必须要引入 -->
<dependency>
<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage</artifactId>
<version>0.7.0</version>
</dependency>
<!-- 七牛云 Kodo 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.11.0</version>
</dependency>
spring:
file-storage: #文件存储配置
default-platform: qiniu-kodo-1 #默认使用的存储平台
thumbnail-suffix: ".min.png" #缩略图后缀,例如【.min.jpg】【.png】
qiniu-kodo: # 七牛云 kodo ,不使用的情况下可以不写
- platform: qiniu-kodo-1 # 存储平台标识
enable-storage: true # 启用存储
access-key: fs36d041cTVsfZQpjcF8nYMJMH79YBmQm8_8Wkws
secret-key: BePLygpLgPn77kxQOqJK-HQo8Ilmb_3rgsCTPDdE
bucket-name: unishop
domain: http://rr051knqp.hn-bkt.clouddn.com/ # 访问域名,注意“/”结尾,例如:http://abc.hn-bkt.clouddn.com/
base-path: shop/ # 基础路径
@Autowired
private FileService fileService;
@ApiOperation("上传文件到七牛云")
@PostMapping("/uploadQiNiu")
public ResData uploadQiNiu(@RequestParam("file") MultipartFile file) {
FileInfo fileInfo = fileService.uploadQiNiu(file);
return ResData.ok("上传文件成功", fileInfo);
}
@ApiOperation("从七牛云下载文件")
@GetMapping("/downloadQiNiuToLocal")
public ResData downloadFile(@RequestParam("openStye") String openStye, @RequestParam("url") String url, HttpServletResponse response) {
fileService.downloadQiNiuToLocal(openStye, url, response);
return ResData.ok("下载文件成功");
}
FileInfo uploadQiNiu(MultipartFile file) throws BusinessException;
void downloadQiNiuToLocal(String open, String url, HttpServletResponse response) throws BusinessException;
class FileServiceImpl extends ServiceImpl<FileMapper, FileDetail> implements FileRecorder, FileService{
/**
* 注入实列
*/
@Resource
private FileStorageService fileStorageService;
@Override
public FileInfo uploadQiNiu(MultipartFile file) throws BusinessException {
return fileStorageService.of(file)
.thumbnail(th -> th.size(80, 80))
.upload();
}
@Override
public void downloadQiNiuToLocal(String open, String url, HttpServletResponse response) throws BusinessException {
FileInfo fileInfo = fileStorageService.getFileInfoByUrl(url);
String filename = fileInfo.getFilename();
//打开方式(inline,attachment)
String openStyle = cn.hutool.core.util.StrUtil.isEmptyIfStr(open) ? "attachment" : open;
fileStorageService.download(fileInfo).inputStream(in -> {
try (OutputStream out = response.getOutputStream()) {
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", openStyle + ";filename=" + URLEncoder.encode(filename, "UTF-8"));
//创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while ((len = in.read(buffer)) > 0) {
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public boolean record(FileInfo fileInfo) {
FileDetail detail = BeanUtil.copyProperties(fileInfo, FileDetail.class, "attr");
try {
//这是手动获 取附加属性字典 并转成 json 字符串,方便存储在数据库中
if (fileInfo.getAttr() != null) {
detail.setAttr(new ObjectMapper().writeValueAsString(fileInfo.getAttr()));
}
} catch (Exception e) {
e.printStackTrace();
}
boolean b = save(detail);
if (b) {
fileInfo.setId(detail.getId());
}
return b;
}
@Override
public FileInfo getByUrl(String url) {
FileDetail detail = getOne(new QueryWrapper<FileDetail>().eq("url", url));
FileInfo info = BeanUtil.copyProperties(detail, FileInfo.class, "attr");
//这是手动获取数据库中的 json 字符串 并转成 附加属性字典,方便使用
if (StrUtil.isNotBlank(detail.getAttr())) {
try {
info.setAttr(new ObjectMapper().readValue(detail.getAttr(), Dict.class));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return info;
}
@Override
public boolean delete(String url) {
return remove(new QueryWrapper<FileDetail>().eq("url", url));
}
}
更多推荐

所有评论(0)