1.add user avatar upload/download interface

main
Gary 1 year ago
parent aeb2f55811
commit be09572659

@ -16,4 +16,18 @@ public class Constants {
public static final String FOLDER_AVATAR = "avatar/";
public static final String MUSIC_RESOURCE_PREFIX="http://wx.gisocn.com:18599/music/";
public static final Integer LENGTH_8 = 8;
public static final Integer LENGTH_10 = 10;
public static final Integer LENGTH_20 = 20;
public static final Integer LENGTH_30 = 30;
public static final Integer LENGTH_50 = 50;
public static final Integer LENGTH_70 = 70;
public static final Integer LENGTH_150 = 150;
}

@ -0,0 +1,55 @@
package util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ScaleFilter {
private static final Logger logger = LoggerFactory.getLogger(ScaleFilter.class);
public static Boolean createThumbnail(File file, int thumbnailWidth, int thumbnailHeight, File targetFile) {
try {
BufferedImage src = ImageIO.read(file);
//thumbnailWidth 缩略图的宽度 thumbnailHeight 缩略图的高度
int sorceW = src.getWidth();
int sorceH = src.getHeight();
//小于 指定高宽不压缩
if (sorceW <= thumbnailWidth) {
return false;
}
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);
}
ImageIO.write(dst, "JPEG", targetFile);
return true;
} catch (Exception e) {
logger.error("生成缩略图失败");
}
return false;
}
public static void main(String[] args) {
createThumbnail(new File("C:\\Users\\Administrator\\Pictures\\20180316193912_VJWJG11.jpeg"), 400, 200,
new File("C:\\Users\\Administrator\\Pictures\\20180316193912_VJWJG12.jpeg"));
}
}

@ -1,24 +1,25 @@
package com.luoo.user.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.io.InputStream;
import java.nio.file.Paths;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import constants.Constants;
import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.pojo.User;
import com.luoo.user.service.UserService;
@ -35,6 +36,7 @@ import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import util.DateUtil;
import util.JwtUtil;
import util.ScaleFilter;
import util.StringTools;
@RestController
@ -47,9 +49,7 @@ public class MyController {
@Autowired
private UserService userService;
/*
* @Value("${project.folder:}") private String projectFolder="./";
*/
public static String UPLOAD_DIRECTORY = System.getProperty("user.dir") + File.separator+"luoo_uploads"+File.separator;
@ApiOperation(value = "1.获取个人信息")
@GetMapping("/getUserInfo")
@ -70,73 +70,75 @@ public class MyController {
return Result.success(userRespDTO);
}
@ApiOperation(value = "2.编辑个人信息",notes="游客无法编辑个人信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "nickName", value = "昵称最多12个字符", required = false),
@ApiOperation(value = "2.编辑个人信息", notes = "游客无法编辑个人信息")
@ApiImplicitParams({ @ApiImplicitParam(name = "nickName", value = "昵称最多12个字符", required = false),
@ApiImplicitParam(name = "signature", value = "签名最多50个字符", required = false),
@ApiImplicitParam(name = "birthDay", value = "生日,格式为: yyyy.MM.dd", required = false),
@ApiImplicitParam(name = "sex", value = "性别4个值0为男1 为女2为保密不填为空", required = false)
})
@ApiImplicitParam(name = "sex", value = "性别4个值0为男1 为女2为保密不填为空", required = false) })
@PostMapping("/updateUserInfo")
@GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> updateUserInfo(@RequestHeader(value = "token", required = false) String token,
@VerifyParam(max = 12) @RequestParam("nickName") String nickName,
@VerifyParam(max = 50)@RequestParam("signature") String signature,
@RequestParam("birthDay") String birthDay,
@RequestParam("sex") String sex) {
@VerifyParam(max = 50) @RequestParam("signature") String signature,
@RequestParam("birthDay") String birthDay, @RequestParam("sex") String sex) {
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
User user = userService.findById(userLoginDto.getUserId());
if(!StringTools.isEmpty(nickName)&&!nickName.equals(user.getNickname())) {
if (!StringTools.isEmpty(nickName) && !nickName.equals(user.getNickname())) {
long count = userService.countByNickName(nickName);
if (count > 0) {
return Result.failed(StatusCode.USER_NICK_NAME_HAS_BEEN_EXIST);
}
}
if(!StringTools.isEmpty(nickName)) {
if (!StringTools.isEmpty(nickName)) {
user.setNickname(nickName);
}
if(!StringTools.isEmpty(signature)) {
if (!StringTools.isEmpty(signature)) {
user.setPersonality(signature);
}
if(!StringTools.isEmpty(birthDay)) {
if (!StringTools.isEmpty(birthDay)) {
user.setBirthday(DateUtil.parse(birthDay, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()));
}
if(!StringTools.isEmpty(sex)) {
if (!StringTools.isEmpty(sex)) {
user.setSex(sex);
}
userService.update(user);
return Result.success();
}
@ApiOperation(value = "3.上传个人头像", notes = "图片压缩为70X70 JPEG存入服务器用户目录/luoo_uploads/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 = jwtUtil.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));
}
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);
User user = userService.findById(userLoginDto.getUserId());
user.setAvatar(Constants.FOLDER_AVATAR + avatarName);
/*
* @ApiOperation(value = "2.上传个人头像")
*
* @RequestMapping("/uploadAvatar")
*
* @GlobalInterceptor(checkLogin = true) public Result
* uploadAvatar(@RequestHeader(value = "token", required = false) String token,
* MultipartFile file) throws IOException { UserLoginDto userLoginDto =
* jwtUtil.getUserLoginDto(token); String folderName = projectFolder +
* Constants.FOLDER_AVATAR; File folder = new File(folderName); if
* (!folder.exists()) { folder.exists(); }
*
* 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);
*
* AppUserInfo appUserInfo = new AppUserInfo();
* appUserInfo.setAvatar(Constants.FOLDER_AVATAR + avatarName);
* appUserInfoService.updateAppUserInfoByUserId(appUserInfo,
* userLoginDto.getUserId()); return getSuccessResponseVO(null); }
*/
userService.update(user);
return Result.success();
}
//待优化
@ApiOperation(value = "4.下载个人头像")
@GetMapping(value = "/avatar", produces = MediaType.IMAGE_JPEG_VALUE)
@GlobalInterceptor(checkLogin = true)
public @ResponseBody byte[] getImage(@RequestHeader(value = "token", required = false) String token)
throws IOException {
UserLoginDto userLoginDto = jwtUtil.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);
}
}

Loading…
Cancel
Save