guns 与 七牛云 同步(前端调用 二)
2020-02-04 本文已影响0人
阳光也学会了妩媚
如上一篇后端一样
(1)注册七牛云--->引入pom--->做个工具类
(2)引用
//根据id上传
@PostMapping("/upload/audio/{id}/id")
@ResponseBody
public ServerResponse uploadAudio(@RequestPart("file") MultipartFile file, @PathVariable(name = "id") String id) {
String fileName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename());
String fileSavePath = ReadUploadPath.getFileUploadPath();
String qunitUrl = "";
try {
InputStream inputStream = file.getInputStream();
qunitUrl = QiniuUtil.UploadFile(fileName, inputStream);
System.out.println("七牛云上传成功");
//sunyuechao
file.transferTo(new File(fileSavePath + fileName));
} catch (Exception e) {
System.out.println("文件上传失败!");
}
SycShop sycShop = iSycShopService.selectById(id);
if (sycShop.getSiteaudio() == null) {
sycShop.setSiteaudio(fileName);
} else {
sycShop.setSiteaudio(sycShop.getSiteimage() + ";" + fileName);
}
System.out.println(sycShop);
iSycShopService.updateById(sycShop);
UploadFileVo fileVo=new UploadFileVo();
fileVo.setId(id);
fileVo.setFullPath(qunitUrl);
return ServerResponse.createBySuccess(fileVo);
}
相关解释:
获取本地路径和七牛云同时存储,这样后端也能访问到本地图片地址
因为前后端分离 前端我用uni 的文件上传组件,需要返回值有id 和 fullpath 字段,我就单独封装了一个vo
这里感觉用sycShop.updatebyid 好像效率要比 iSycShopService.updateById(sycShop); 高。