掘金项目之用户首页

<%--
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" %>


Title








<%@include file="common/header.jsp"%>
<%--${userinfo.username}的个人主页--%>
<%----%>
<%--编辑个人信息--%>














后台

@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";
}

你可能感兴趣的:(掘金项目之用户首页)