Commit 85932501 by fsn

头像上传机制的变更

parent 6d1a9837
......@@ -18,9 +18,4 @@ public interface Const {
*/
String ADMIN_NAME = "administrator";
/**
* 默认头像
*/
String DEFAULT_AVATAR = "profile_small.jpg";
}
......@@ -3,6 +3,9 @@ package com.stylefeng.guns.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import static com.stylefeng.guns.core.util.ToolUtil.getTempPath;
import static com.stylefeng.guns.core.util.ToolUtil.isEmpty;
/**
* guns项目配置
*
......@@ -17,6 +20,21 @@ public class GunsProperties {
private Boolean kaptchaOpen;
private String fileUploadPath;
public String getFileUploadPath() {
//如果没有写文件上传路径
if(isEmpty(fileUploadPath)){
return getTempPath();
}else{
return fileUploadPath;
}
}
public void setFileUploadPath(String fileUploadPath) {
this.fileUploadPath = fileUploadPath;
}
public Boolean getKaptchaOpen() {
return kaptchaOpen;
}
......
......@@ -534,4 +534,29 @@ public class ToolUtil {
public static Boolean getKaptchaOnOff(){
return SpringContextHolder.getBean(GunsProperties.class).getKaptchaOpen();
}
/**
* 判断是否是windows操作系统
*
* @author stylefeng
* @Date 2017/5/24 22:34
*/
public static Boolean isWinOs(){
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
return true;
}else{
return false;
}
}
/**
* 获取临时目录
*
* @author stylefeng
* @Date 2017/5/24 22:35
*/
public static String getTempPath(){
return System.getProperty("java.io.tmpdir");
}
}
\ No newline at end of file
......@@ -2,10 +2,14 @@ package com.stylefeng.guns.modular.system.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
......@@ -24,6 +28,9 @@ import java.io.IOException;
@RequestMapping("/kaptcha")
public class KaptchaController {
@Resource
private GunsProperties gunsProperties;
@Autowired
Producer producer;
......@@ -83,4 +90,26 @@ public class KaptchaController {
}
}
}
/**
* 返回图片
*
* @author stylefeng
* @Date 2017/5/24 23:00
*/
@RequestMapping("/{pictureId}")
public void renderPicture(@PathVariable("pictureId") String pictureId, HttpServletResponse response) {
String path = gunsProperties.getFileUploadPath() + pictureId + ".jpg";
try {
byte[] bytes = FileUtil.toByteArray(path);
response.getOutputStream().write(bytes);
}catch (Exception e){
//如果找不到图片就返回一个默认图片
try {
response.sendRedirect("/static/img/girl.gif");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
package com.stylefeng.guns.modular.system.controller;
import com.google.code.kaptcha.Constants;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.InvalidKaptchaException;
import com.stylefeng.guns.common.node.MenuNode;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.MenuDao;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.User;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -55,9 +54,6 @@ public class LoginController extends BaseController {
Integer id = ShiroKit.getUser().getId();
User user = userMapper.selectById(id);
String avatar = user.getAvatar();
if(ToolUtil.isEmpty(avatar)){
avatar = Const.DEFAULT_AVATAR;
}
model.addAttribute("avatar", avatar);
return "/index.html";
......
......@@ -12,8 +12,8 @@ import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.db.Db;
import com.stylefeng.guns.core.listener.ConfigListener;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
......@@ -50,6 +50,9 @@ public class UserMgrController extends BaseController {
private static String PREFIX = "/system/user/";
@Resource
private GunsProperties gunsProperties;
@Resource
private UserMgrDao managerDao;
@Resource
......@@ -310,7 +313,7 @@ public class UserMgrController extends BaseController {
String upload(@RequestPart("file") MultipartFile picture) {
String pictureName = UUID.randomUUID().toString() + ".jpg";
try {
String fileSavePath = ConfigListener.getConf().get("realPath") + "static\\img\\";
String fileSavePath = gunsProperties.getFileUploadPath();
picture.transferTo(new File(fileSavePath + pictureName));
} catch (Exception e) {
throw new BussinessException(BizExceptionEnum.UPLOAD_ERROR);
......
################### guns项目独有的配置 ###################
guns:
kaptcha-open: false #是否开启登录时验证码 (true/false)
file-upload-path:
################### 项目启动端口 ###################
server:
......
......@@ -5,7 +5,13 @@
<ul class="nav" id="side-menu">
<li class="nav-header">
<div class="dropdown profile-element">
<span><img alt="image" class="img-circle" src="${ctxPath}/static/img/${avatar}" width="64px" height="64px"/></span>
<span><img alt="image" class="img-circle"
@if(isEmpty(avatar)){
src="${ctxPath}/static/img/girl.gif"
@}else{
src="${ctxPath}/kaptcha/${avatar}"
@}
width="64px" height="64px"/></span>
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs"><strong class="font-bold">${shiro.getUser().name}</strong></span>
......
......@@ -11,7 +11,7 @@
@if(isEmpty(avatarImg)){
src="${ctxPath}/static/img/girl.gif"></div>
@}else{
src="${ctxPath}/static/img/${avatarImg}"></div>
src="${ctxPath}/kaptcha/${avatarImg}"></div>
@}
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment