后端接口接收数据和业务代码
@PostMapping("/uploadphoto")
@OperationLog(name = "照片上传", type = OperationLogType.UPLOAD)
@ApiOperation(value = "照片上传", response = ApiResult.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "file", value = "文件", required = true),
@ApiImplicitParam(name = "photoSizeId", value = "照片尺寸ID", required = true),
@ApiImplicitParam(name = "subfixFile", value = "照片后缀 如 .png", required = true)
})
public ApiResult uploadPhoto(
@RequestParam("photoSizeId") String photoSizeId,
@RequestParam("advanced") String advanced,
@RequestParam("subfixFile") String subfixFile,
@RequestParam(value = "file") MultipartFile file)
throws Exception {
Long userId = DeviceQueryUtil.getUserId();
if (userId.equals(-1L)
|| !StringUtils.isNumeric(photoSizeId)
|| !StringUtils.isNumeric(advanced)
|| StringUtils.isBlank(subfixFile)
|| file.isEmpty()) {
return ApiResult.result(ApiCode.FAIL, "信息错误", null);
}
// 获取传过来的文件名
String fileName = file.getOriginalFilename();
UploadPhotoParam uploadPhotoParam =
photoStorageService.uploadPhoto(file, fileName, subfixFile, photoSizeId, userId, advanced);
if (!StringUtils.isBlank(uploadPhotoParam.getThumbnailsUrl())
&& uploadPhotoParam.getPhotoStorageId() > 0L) {
return ApiResult.result(ApiCode.SUCCESS, "照片上传成功", uploadPhotoParam);
}
return ApiResult.result(ApiCode.FAIL, "照片上传失败", null);
}
@Override
public UploadPhotoParam uploadPhoto(
MultipartFile file,
String fileName,
String subfixFile,
String photoSizeId,
Long userId,
String advanced)
throws IOException {
// 通过UUID和后缀名拼接新的文件名
String newFileName = userId + "/" + IdUtil.simpleUUID() + subfixFile;
String ThumbnailsName = userId + "/" + IdUtil.simpleUUID() + subfixFile;
UploadPhotoParam uploadPhotoParam = new UploadPhotoParam();
// 上传文件流。
try {
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// 上传网络流。
InputStream inputStream = file.getInputStream();
PutObjectResult putObjectResult = obsClient.putObject(bucketName, newFileName, inputStream);
String Url = putObjectResult.getObjectUrl();
// 缩略照片上传
ByteArrayOutputStream os = new ByteArrayOutputStream();
Thumbnails.of(file.getInputStream()).forceSize(49, 60).toOutputStream(os);
ByteArrayInputStream ThumbnailsInputStream = ImageUtil.parseToInputStream(os);
putObjectResult = obsClient.putObject(bucketName, ThumbnailsName, ThumbnailsInputStream);
String ThumbnailsUrl = putObjectResult.getObjectUrl();
// 关闭流
inputStream.close();
os.close();
// 关闭obsClient
obsClient.close();
// 保存照片信息
PhotoStorage photoStorage = new PhotoStorage();
// 上传原文件名称
photoStorage.setPhotoName(fileName);
photoStorage.setPhotoSizeId(Integer.parseInt(photoSizeId));
// 只存照片的名称 域名是可配置的
photoStorage.setUrl(newFileName);
photoStorage.setThumbnailsUrl(ThumbnailsName);
photoStorage.setStatus(PhotoStorageType.NoPay.code);
photoStorage.setUserId(userId);
photoStorage.setAdvanced(Integer.parseInt(advanced));
this.save(photoStorage);
uploadPhotoParam.setPhotoStorageId(photoStorage.getId());
uploadPhotoParam.setThumbnailsUrl(ThumbnailsUrl);
return uploadPhotoParam;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return uploadPhotoParam;
}
核心代码
<!-- 华为云-->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.20.6.1</version>
</dependency>
<!--thumbnailator 压缩工具-->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
</dependencies>
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// 上传网络流。
InputStream inputStream = file.getInputStream();
PutObjectResult putObjectResult = obsClient.putObject(bucketName, newFileName, inputStream);
String Url = putObjectResult.getObjectUrl();
// 缩略照片上传
ByteArrayOutputStream os = new ByteArrayOutputStream();
Thumbnails.of(file.getInputStream()).forceSize(49, 60).toOutputStream(os);
ByteArrayInputStream ThumbnailsInputStream = ImageUtil.parseToInputStream(os);
putObjectResult = obsClient.putObject(bucketName, ThumbnailsName, ThumbnailsInputStream);
String ThumbnailsUrl = putObjectResult.getObjectUrl();
// 关闭流
inputStream.close();
os.close();
// 关闭obsClient
obsClient.close();
这边首先创建华为云客户端,然后将照片发送到华为云。然后使用Thumbnails框架,处理照片。再将照片转为流再发送照片到华为云。再缩略图的部分有流转换的过程。
public static ByteArrayInputStream parseToInputStream(final OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
baos.close();
out.close();
swapStream.close();
return swapStream;
}