use of com.ruoyi.resource.api.domain.SysFile in project RuoYi-Cloud-Plus by JavaLionLi.
the class SysProfileController method avatar.
/**
* 头像上传
*/
@GlobalTransactional(rollbackFor = Exception.class)
@ApiOperation("头像上传")
@ApiImplicitParams({ @ApiImplicitParam(name = "avatarfile", value = "用户头像", paramType = "query", dataTypeClass = File.class, required = true) })
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping("/avatar")
public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
SysFile sysFile = remoteFileService.upload(file.getName(), file.getOriginalFilename(), file.getContentType(), file.getBytes());
if (ObjectUtil.isNull(sysFile)) {
return R.fail("文件服务异常,请联系管理员");
}
String url = sysFile.getUrl();
if (userService.updateUserAvatar(LoginHelper.getUsername(), url)) {
Map<String, Object> ajax = new HashMap<>();
ajax.put("imgUrl", url);
return R.ok(ajax);
}
}
return R.fail("上传图片异常,请联系管理员");
}
use of com.ruoyi.resource.api.domain.SysFile in project RuoYi-Cloud-Plus by JavaLionLi.
the class RemoteFileServiceImpl method upload.
/**
* 文件上传请求
*/
@Transactional(rollbackFor = Exception.class)
@Override
public SysFile upload(String name, String originalFilename, String contentType, byte[] file) throws ServiceException {
try {
String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
OssClient storage = OssFactory.instance();
UploadResult uploadResult = storage.uploadSuffix(file, suffix, contentType);
// 保存文件信息
SysOss oss = new SysOss();
oss.setUrl(uploadResult.getUrl());
oss.setFileSuffix(suffix);
oss.setFileName(uploadResult.getFilename());
oss.setOriginalName(originalFilename);
oss.setService(storage.getConfigKey());
sysOssMapper.insert(oss);
SysFile sysFile = new SysFile();
sysFile.setName(uploadResult.getFilename());
sysFile.setUrl(uploadResult.getUrl());
return sysFile;
} catch (Exception e) {
log.error("上传文件失败", e);
throw new ServiceException("上传文件失败");
}
}
Aggregations