1.update avatar for comment

main
Gary 10 months ago
parent ed8f8dec62
commit ad0e43cc7a

@ -5,16 +5,16 @@ public class Constants {
public static final String REDIS_KEY_IMAGE_CHECK_CODE = "redis_key_image_check_code_";
public static final String REDIS_KEY_MOBILE_CHECK_CODE = "redis_key_mobile_check_code_";
public static final String REDIS_KEY_USER_COLLECT_JOURNAL = "redis_key_user_collect_journal_";
public static final String REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO = "redis_key_page_journal_response_dto___";
public static final String J2CACHE_REGION_JOURNAL_QUERY_PAGE = "journal_query_page";
public static final String J2CACHE_REGION_JOURNAL_ID = "journal_id";
public static final String J2CACHE_REGION_JOURNAL_NO = "journal_no";
public static final String J2CACHE_REGION_JOURNAL_SONG_LIST = "journal_song_list";
public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT = "journal_comment_page_hot";
public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW = "journal_comment_page_new";
@ -28,7 +28,7 @@ public class Constants {
public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();
public static final String TOKEN_ROLE = "roles";
public static final String TOKEN_ROLE_APP_USER = "user";
@ -38,13 +38,16 @@ public class Constants {
public static final String RESOURCE_PREFIX = "http://cdn1.indie.cn:19000/indie/";
public static final String MUSIC_RESOURCE_PREFIX = RESOURCE_PREFIX + "music/";
public static final String SONG_RESOURCE_PREFIX = RESOURCE_PREFIX + "song/";
public static final String TAG_RESOURCE_PREFIX = RESOURCE_PREFIX + "tag/";
public static final String DEFAULT_USER_AVATAR = "user/avatar/default_20240227_232949.jpg";
public static final String DEFAULT_USER_SIGNATURE = "独立 不独于世";
public static String USER_AVATAR_DIRECTORY = "user/avatar/";
public static String USER_FEEDBACK_IMAGE_DIRECTORY = "user/feedback/";
public static final Integer LENGTH_8 = 8;
public static final Integer LENGTH_10 = 10;
@ -58,10 +61,10 @@ public class Constants {
public static final Integer LENGTH_70 = 70;
public static final Integer LENGTH_150 = 150;
public static final Integer LENGTH_200 = 200;
public static final Integer LENGTH_512 = 512;
public static final String IP_LOCATION_CHINA = "中国";
}

@ -9,85 +9,58 @@ import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
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;
public static byte[] createThumbnail(URL url, int thumbnailWidth, int thumbnailHeight) {
try {
BufferedImage src = ImageIO.read(url);
return createThumbnail(src,thumbnailWidth,thumbnailHeight);
} 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"));
}
return null;
}
private static byte[] createThumbnail(BufferedImage src, int thumbnailWidth, int thumbnailHeight) throws IOException {
//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();
}
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();
return createThumbnail(src,thumbnailWidth,thumbnailHeight);
} catch (Exception e) {
logger.error("生成缩略图失败");
}

@ -102,9 +102,6 @@ public class MyController extends BaseController {
@Autowired
private RedisTemplate redisTemplate;
public static String USER_AVATAR_DIRECTORY = "user/avatar/";
public static String USER_FEEDBACK_IMAGE_DIRECTORY = "user/feedback/";
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
@GetMapping("/userInfo")
@GlobalInterceptor(checkAppUserLogin = true)
@ -134,8 +131,6 @@ public class MyController extends BaseController {
if (count > 0) {
return Result.failed(StatusCode.USER_NICK_NAME_HAS_BEEN_EXIST);
}
}
if (!StringTools.isEmpty(nickName)) {
@ -172,7 +167,7 @@ public class MyController extends BaseController {
String fileId=userLoginDto.getUserId() + "_" + idWorker.nextId();
String avatarName = fileId
+ StringTools.getFileSuffix(file.getOriginalFilename());
String avatarFilePath = USER_AVATAR_DIRECTORY + avatarName;
String avatarFilePath = Constants.USER_AVATAR_DIRECTORY + avatarName;
s3Service.uploadImage("indie", avatarFilePath, avatar);
@ -183,7 +178,7 @@ public class MyController extends BaseController {
if(null!=thumbnail) {
String thumbnailName = fileId+"_thumbnail"
+ StringTools.getFileSuffix(file.getOriginalFilename());
String thumbnailFilePath = USER_AVATAR_DIRECTORY + thumbnailName;
String thumbnailFilePath = Constants.USER_AVATAR_DIRECTORY + thumbnailName;
s3Service.uploadImage("indie", thumbnailFilePath, thumbnail);
user.setThumbnail(thumbnailFilePath);
}else {
@ -291,7 +286,7 @@ public class MyController extends BaseController {
private String uploadImages(Feedback feedback, MultipartFile file) {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId()
+ StringTools.getFileSuffix(file.getOriginalFilename());
String filePath = USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
s3Service.uploadImage("indie", filePath, file.getBytes());
return imageName;
}

@ -74,6 +74,9 @@ public class SimpleUserController {
@GetMapping("/user/{id}")
public UserInfo findByUserId(@PathVariable String id) {
UserInfo userInfo = userInfoService.findById(id);
if(!StringTools.isEmpty(userInfo.getThumbnail())) {
userInfo.setAvatar(userInfo.getThumbnail());
}
return userInfo;
}

Loading…
Cancel
Save