1.upload avatar to s3

main
Gary 1 year ago
parent ba7b0b8779
commit 9e130d628e

@ -2,11 +2,14 @@ package util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
public class ScaleFilter {
private static final Logger logger = LoggerFactory.getLogger(ScaleFilter.class);
@ -52,4 +55,42 @@ public class ScaleFilter {
createThumbnail(new File("C:\\Users\\Administrator\\Pictures\\20180316193912_VJWJG11.jpeg"), 400, 200,
new File("C:\\Users\\Administrator\\Pictures\\20180316193912_VJWJG12.jpeg"));
}
public static byte[] createThumbnail(InputStream io, int thumbnailWidth, int thumbnailHeight) {
try {
BufferedImage src = ImageIO.read(io);
//thumbnailWidth 缩略图的宽度 thumbnailHeight 缩略图的高度
int sorceW = src.getWidth();
int sorceH = src.getHeight();
//小于 指定高宽不压缩
if (sorceW <= thumbnailWidth) {
return null;
}
int height; // 目标文件的高度
if (sorceW > thumbnailWidth) { // 目标文件宽度大于指定宽度
height = thumbnailWidth * sorceH / sorceW;
} else {// 目标文件宽度小于指定宽度 那么缩略图大小就跟原图一样大
thumbnailWidth = sorceW;
height = sorceH;
}
BufferedImage dst = new BufferedImage(thumbnailWidth, height, BufferedImage.TYPE_INT_RGB);
Image scaleImage = src.getScaledInstance(thumbnailWidth, height, Image.SCALE_SMOOTH);
Graphics2D g = dst.createGraphics();
g.drawImage(scaleImage, 0, 0, thumbnailWidth, height, null);
g.dispose();
int resultH = dst.getHeight();
// 高度过大的,裁剪图片
if (resultH > thumbnailHeight) {
resultH = thumbnailHeight;
dst = dst.getSubimage(0, 0, thumbnailWidth, resultH);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(dst, "JPEG", os);
return os.toByteArray();
} catch (Exception e) {
logger.error("生成缩略图失败");
}
return null;
}
}

@ -25,6 +25,7 @@ import controller.BaseController;
import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.pojo.UserInfo;
import com.luoo.user.service.S3Service;
import com.luoo.user.service.UserInfoService;
import annotation.GlobalInterceptor;
@ -47,9 +48,11 @@ import util.StringTools;
@Api(tags = "MyController")
public class MyController extends BaseController{
@Autowired
private S3Service s3Service;
@Autowired
private UserInfoService userInfoService;
public static String UPLOAD_DIRECTORY = System.getProperty("user.dir") + File.separator+"luoo_uploads"+File.separator;
public static String UPLOAD_DIRECTORY = "user/avatar/";
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
@GetMapping("/getUserInfo")
@ -104,38 +107,22 @@ public class MyController extends BaseController{
return Result.success();
}
@ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG存入服务器用户目录/luoo_uploads/avatar/")
@ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG存入S3桶为indie目录为 user/avatar/")
@PostMapping("/uploadAvatar")
@GlobalInterceptor(checkLogin = true)
public Result<Void> uploadAvatar(@RequestHeader(value = "token", required = false) String token,
@VerifyParam(required = true) MultipartFile file) throws IOException {
UserLoginDto userLoginDto = getUserLoginDto(token);
String folderName = UPLOAD_DIRECTORY + Constants.FOLDER_AVATAR;
File folder = new File(folderName);
if (!folder.exists()) {
java.nio.file.Files.createDirectories(Paths.get(folderName));
}
byte[] thumbnail =ScaleFilter.createThumbnail(file.getInputStream(), Constants.LENGTH_70, Constants.LENGTH_70);
String avatarName = userLoginDto.getUserId() + StringTools.getFileSuffix(file.getOriginalFilename());
File avatarFileName = new File(folder.getPath() + "/" + avatarName);
file.transferTo(avatarFileName);
ScaleFilter.createThumbnail(avatarFileName, Constants.LENGTH_70, Constants.LENGTH_70, avatarFileName);
String filePath=UPLOAD_DIRECTORY+avatarName;
s3Service.uploadAvatar("indie",filePath,thumbnail);
UserInfo user = userInfoService.findById(userLoginDto.getUserId());
user.setAvatar(Constants.FOLDER_AVATAR + avatarName);
user.setAvatar(filePath);
userInfoService.update(user);
return Result.success();
}
//待优化
@ApiOperation(value = "4.下载个人头像")
@GetMapping(value = "/downloadAvatar", produces = MediaType.IMAGE_JPEG_VALUE)
@GlobalInterceptor(checkLogin = true)
public @ResponseBody byte[] getImage(@RequestHeader(value = "token", required = false) String token)
throws IOException {
UserLoginDto userLoginDto = getUserLoginDto(token);
String folderName = UPLOAD_DIRECTORY + "avatar";
File folder = new File(folderName);
File avatarFileName = new File(folder.getPath() + File.separator + userLoginDto.getUserId() + ".JPG");
InputStream in = new FileInputStream(avatarFileName);
return IOUtils.toByteArray(in);
}
}

@ -12,6 +12,8 @@ import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@ -95,4 +97,13 @@ public class S3Service {
return 1;
}
public void uploadAvatar(String bucket, String key, byte[] buffer) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.key(key)
.build();
RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length);
s3Client.putObject(putObjectRequest, requestBody);
}
}

Loading…
Cancel
Save