From 418678a2f6eeeb9de97795a26277d7c5c64c282d Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期四, 23 五月 2019 15:13:03 +0800 Subject: [PATCH] 用户头像压缩200x200 --- fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.java | 72 ++++++++++++++++++++++++++--------- 1 files changed, 53 insertions(+), 19 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.java index 99b30c6..9d92fbd 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.java @@ -1,5 +1,6 @@ package com.yeshi.fanli.service.impl.user; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; @@ -19,11 +20,13 @@ import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; +import org.springframework.core.task.TaskExecutor; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import org.yeshi.utils.FileUtil; import org.yeshi.utils.HttpUtil; import org.yeshi.utils.NumberUtil; import org.yeshi.utils.tencentcloud.COSManager; @@ -49,6 +52,7 @@ import com.yeshi.fanli.service.inter.count.HongBaoV2CountService; import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce; import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionAuthRecordService; +import com.yeshi.fanli.service.inter.user.SpreadUserImgService; import com.yeshi.fanli.service.inter.user.UserActiveLogService; import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService; import com.yeshi.fanli.service.inter.user.UserInfoService; @@ -62,9 +66,14 @@ import com.yeshi.fanli.util.wx.WXLoginUtil; import com.yeshi.fanli.vo.user.UserInfoVO; +import net.coobird.thumbnailator.Thumbnails; + @Service public class UserInfoServiceImpl implements UserInfoService { + @Resource(name = "taskExecutor") + private TaskExecutor executor; + @Resource private UserInfoDao userInfoDao; @@ -109,6 +118,11 @@ @Resource private TaoBaoUnionAuthRecordService taoBaoUnionAuthRecordService; + + @Resource + private SpreadUserImgService spreadUserImgService; + + public UserInfo getUserByLoginTypeAndOpenId(int loginType, String openid, String appid) { List<UserInfo> list = null; @@ -910,31 +924,53 @@ } @Override - public void uploadPortrait(MultipartFile file,Long uid) throws UserInfoException, IOException { + public void uploadPortrait(MultipartFile file, Long uid) throws UserInfoException, IOException { UserInfo userInfo = userInfoMapper.selectByPKey(uid); if(userInfo == null) { throw new UserInfoException(1, "鐢ㄦ埛涓嶅瓨鍦�"); } + // 璋锋瓕鍘嬬缉鍥剧墖 + String targetPath = FileUtil.getCacheDir() + "/uploadPortrait_" + uid + "_" + System.currentTimeMillis() + ".jpg"; + Thumbnails.of(file.getInputStream()).size(200, 200).toFile(targetPath); + + // 鎵ц涓婁紶 + String filePath="/img/user/" + UUID.randomUUID().toString().replace("-", "") + ".jpg"; + String fileLink= COSManager.getInstance().uploadFile(new File(targetPath), filePath).getUrl(); + + // 鍒犻櫎鏈湴鍥剧墖 + if (new File(targetPath).exists()) { + new File(targetPath).delete(); + } + + // 鍘熷ご鍍� String portrait = userInfo.getPortrait(); - if (portrait != null && portrait.trim().length() > 0 && portrait.contains("/img/user/")) { + + // 鏇存柊淇℃伅 + userInfo.setPortrait(fileLink); + userInfoMapper.updateByPrimaryKeySelective(userInfo); + + // 鍒犻櫎鍘熷ご鍍� + if (!StringUtil.isNullOrEmpty(portrait) && portrait.contains("/img/user/")) { COSManager.getInstance().deleteFile(portrait); } - - // 鏂囦欢瑙f瀽 - InputStream inputStream = file.getInputStream(); - String contentType = file.getContentType(); - String type = contentType.substring(contentType.indexOf("/") + 1); - - // 鏂囦欢璺緞 - String filePath="/img/user/"+UUID.randomUUID().toString().replace("-", "") + "." + type; - // 鎵ц涓婁紶 - String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl(); - - userInfo.setPortrait(fileLink); - userInfoMapper.updateByPrimaryKey(userInfo); + executor.execute(new Runnable() { + @Override + public void run() { + try { + // 鍒犻櫎鍒嗕韩澶村儚淇℃伅 + spreadUserImgService.deleteImgUrl(uid); + } catch (Exception e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + }); } @@ -945,9 +981,7 @@ throw new UserInfoException(1, "鐢ㄦ埛涓嶅瓨鍦�"); } - UserInfo updateInfo = new UserInfo(); - updateInfo.setId(userInfo.getId()); - updateInfo.setNickName(nickName); - userInfoMapper.updateByPrimaryKeySelective(updateInfo); + userInfo.setNickName(nickName); + userInfoMapper.updateByPrimaryKeySelective(userInfo); } } -- Gitblit v1.8.0