<%--
Created by IntelliJ IDEA.
User: ttc
Date: 2018/3/29
Time: 11:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link href="${pageContext.request.contextPath}/assets/uikit-2.25.0/css/uikit.almost-flat.css?ver=2.0" rel="stylesheet">
<link href="${pageContext.request.contextPath}/assets/uikit-2.25.0/css/components/form-file.almost-flat.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/assets/css/style.css?ver=2.0" rel="stylesheet">
<script src="${pageContext.request.contextPath}/assets/js/jquery-3.3.1.min.js"></script>
<script src="${pageContext.request.contextPath}/assets/uikit-2.25.0/js/uikit.js"></script>
<script>
$(function () {
$('.uk-tab').on('change.uk.tab',function (event, item) {
alert(item.text());
})
})
</script>
<script>
function upfile() {
var pic = document.getElementsByTagName('input')[0].files[0];
var fd = new FormData();
var xhr = new XMLHttpRequest();
xhr.open('post', '${pageContext.request.contextPath}/currentuser/upload_avatar', true);
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
var res = JSON.parse(this.responseText);
document.getElementById('avatar').src = '${pageContext.request.contextPath}/upload/' + res.src;
}
}
fd.append('avatar', pic);
xhr.send(fd);
}
</script>
</head>
<body>
<%@include file="common/header.jsp"%>
<%--${userinfo.username}的个人主页--%>
<%--<img src="${pageContext.request.contextPath}/upload/${userinfo.avatar}">--%>
<%--<a href="${pageContext.request.contextPath}/currentuser/edit">编辑个人信息</a>--%>
<div class="app-height-75">
<div id="list-body" class="list-body">
<ul class="bookshelf">
<li class="uk-list uk-list-line uk-clearfix topic uk-margin-top">
<div class="content">
<div class="uk-grid">
<div class="uk-width-5-6">
<a href="/topic/show/1">电风扇的说法</a>
<a href="/topic/modify/1">修改</a>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="uk-width-medium-1-4" id="mb_list_right">
<div class="uk-panel uk-panel-box uk-panel-box-secondary">
<div class="uk-grid">
<div class="uk-width-1-2 uk-text-center uk-text-large">
<a href="/user/59693b86518825490d1dd04f/following">
<div data-v-4cdfa676="" class="item-title uk-text-muted">关注了</div>
<div data-v-4cdfa676="" class="item-count uk-text-muted">8</div>
</a>
</div>
<div class="uk-width-1-2 uk-text-center uk-text-large">
<a href="/user/59693b86518825490d1dd04f/followers">
<div data-v-4cdfa676="" class="item-title uk-text-muted">关注者</div>
<div data-v-4cdfa676="" class="item-count uk-text-muted">0</div>
</a>
</div>
</div>
</div>
<div class="b20"></div>
<!--页脚部分-->
</div>
</div>
</div>
</div>
</body>
</html>
后台
@RequestMapping("upload_avatar")
public void upload_avatar(@RequestParam MultipartFile avatar, HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = request.getServletContext().getRealPath("/upload/");
File folder = new File(path);
if(!folder.exists())
{
folder.mkdirs();
}
UUID uuid = UUID.randomUUID();
String strUUid = uuid.toString();
String strFinalPath = path + "/" + strUUid + avatar.getOriginalFilename();
//将头像保存路径保存到userinfo表中
//userinfo表中插入一条记录
HttpSession httpSession = request.getSession();
UserInfo userInfo = (UserInfo)httpSession.getAttribute("userinfo");
userInfo.setAvatar(strUUid + avatar.getOriginalFilename());
userService.updateUser(userInfo);
httpSession.setAttribute("userinfo",userInfo);
File file = new File(strFinalPath);
avatar.transferTo(file);
UploadRes uploadRes = new UploadRes();
uploadRes.setSrc(strUUid + avatar.getOriginalFilename());
String strJson = JSON.toJSONString(uploadRes);
response.getWriter().println(strJson);
// return "redirect:/currentuser/me";
}