牛客网项目---1.7.账号设置(上传头像、修改密码)

7、账号设置/setting

上传头像将头像保存到本地/七牛云服务器,随机生成字符串作为图片的名称,表单提交再返回该界面。获取头像时,在本地获取,通过输入输出流,先读到缓冲区,再从缓冲区写出;在七牛云服务器获取时,不使用流的方式,直接通过文件名获取,将header的URL更新在user用户中。

8、更改密码/changePassword:表单提交新密码,判断密码是否符合标准,更新user信息,并退出登录,重定向到登录界面重新登录

---------------------------------------------------------------------------------------------------------------------------------

1.UserController

package com.nowcoder.community.controller;import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.UserServiceImpl;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;@Controller
public class UserController {private static final Logger logger= LoggerFactory.getLogger(UserController.class);@Value("${community.path.upload}")private String uploadPath;@Value("${community.path.domain}")private String domain;@Value("${server.servlet.context-path}")private String contextPath;@Autowiredprivate UserServiceImpl userService;@Autowiredprivate HostHolder hostHolder;@LoginRequired@GetMapping("/setting")public String getSettingPage(){return "/site/setting";}@LoginRequired@PostMapping("/upload")public String uploadHeader(MultipartFile headerImage, Model model){if (headerImage==null){model.addAttribute("error","您还没有选择图片!");return "/site/setting";}String fileName=headerImage.getOriginalFilename();String suffix = fileName.substring(fileName.lastIndexOf("."));if (StringUtils.isBlank(suffix)){model.addAttribute("error","文件格式不正确!");return "/site/setting";}//生成随机文件名fileName= CommunityUtil.generateUUID()+suffix;//确定文件存放的路径File file1 = new File(uploadPath);if (!file1.exists()){file1.mkdir();}File file = new File(file1 + "/" + fileName);try {//存储文件headerImage.transferTo(file);} catch (IOException e) {logger.error("上传文件失败:"+e.getMessage());throw new RuntimeException("上传文件失败,服务器发生异常!",e);}//更新当前用户的头像路径(web访问路径)// http://localhost:8080/community/header/xxx.pngUser user = hostHolder.getUser();String headerUrl=domain+contextPath+"/header/"+fileName;userService.updateHeader(user.getId(),headerUrl);return "redirect:/index";}@GetMapping("/header/{fileName}")public void getHeader(@PathVariable("fileName") String fileName, HttpServletResponse response){//服务器存放路径fileName=uploadPath+"/"+fileName;//文件后缀String suffix = fileName.substring(fileName.lastIndexOf("."));//响应图片response.setContentType("image/"+suffix);try (FileInputStream fis = new FileInputStream(fileName);ServletOutputStream os = response.getOutputStream();){byte[] bytes=new byte[1024];int len=0;while ((len=fis.read(bytes))!=-1){os.write(bytes,0,len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {logger.error("读取头像失败:"+e.getMessage());}}@LoginRequired@PostMapping("/updatePassword")public String updatePassword(String originalPassword,String newPassword,String confirmPassword,Model model){if (originalPassword==null){model.addAttribute("originalPasswordMsg","请输入原始密码!");return "/site/setting";}if (newPassword==null){model.addAttribute("newPasswordMsg","请输入新密码!");return "/site/setting";}if (confirmPassword==null){model.addAttribute("confirmPasswordMsg","请确认密码!");return "/site/setting";}//确认账号User user = hostHolder.getUser();if (!CommunityUtil.md5(originalPassword+user.getSalt()).equals(user.getPassword())){model.addAttribute("originalPasswordMsg","密码错误!");return "/site/setting";}if (!confirmPassword.equals(newPassword)){model.addAttribute("confirmPasswordMsg","两次输入的密码不一致!");return "/site/setting";}userService.updatePassword(user.getId(),CommunityUtil.md5(newPassword+user.getSalt()));return "redirect:/index";}
}

2.setting.html 

				<form class="mt-5" method="post" enctype="multipart/form-data" th:action="@{/upload}"><div class="form-group row mt-4"><label for="head-image" class="col-sm-2 col-form-label text-right">选择头像:</label><div class="col-sm-10"><div class="custom-file"><input type="file" th:class="|custom-file-input ${error!=null ? 'is-invalid' : ''}|" name="headerImage" id="head-image" lang="es" required=""><label class="custom-file-label" for="head-image" data-browse="文件">选择一张图片</label><div class="invalid-feedback" th:text="${error}">该账号不存在!</div></div></div></div><div class="form-group row mt-4"><div class="col-sm-2"></div><div class="col-sm-10 text-center"><button type="submit" class="btn btn-info text-white form-control">立即上传</button></div></div></form>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/350164.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

Discuz论坛无法上传头像/ 企业邮箱被归为垃圾邮件的问题

背景&#xff1a; 安装了Discuz论坛程序&#xff0c;更改个人头像的时候&#xff0c;点击上传按钮没有反应基于阿里云企业邮箱配置了论坛的邮件发送服务&#xff0c;但是邮箱被列入垃圾邮件拒收 解决方法&#xff1a; 上传头像失败&#xff1a;ucenter和全局域名要加https&a…

flask更改用户头像

目录结构图: 配置app 导包及配置 import os from flask import Flask, flash, request, redirect, url_for,Request,render_template from werkzeug.utils import secure_filenameUPLOAD_FOLDER 文件下载的绝对路径 # 允许的下载类型 ALLOWED_EXTENSIONS {txt, pdf, png, j…

discuz论坛用户--设置--修改头像不显示的解决方法

本文参考&#xff1a;https://www.2cto.com/kf/201601/478810.html 和https://jingyan.baidu.com/article/ceb9fb10ab73b68cad2ba092.html 先登陆discuz管理员后台admin.php&#xff0c;找到“站长”>>“UCenter设置”&#xff0c;其它的默认的地方就不要动了。关键要改…

关于Discuz 出现上传头像失败的问题

今天测试了论坛上传头像时出现 网上大部分的解决方案是配置UCenter Access denied for agent changed 头像无法更新 配置文件&#xff1a; config下config_global.php&#xff0c;config_ucenter.php uc_server\data下的config.inc.php 具体可参考此处&#xff1a;Discuz配…

内部论坛系统

开发环境/技术&#xff1a; Linux&#xff0c;MyEclipse&#xff0c;JDK1.7&#xff0c;MySql&#xff0c;JS&#xff0c;jQuery&#xff0c; ajax&#xff0c;Tomcat&#xff0c;CSS样式&#xff0c;Struts框架等 项目描述/功能&#xff1a; 项目主要实现了用户登陆注册&#…

Vue 彩色头像|一个有趣的头像生成器 附源码

前言 这是一款矢量风格的头像生成器&#xff0c;您可以搭配不同的素材组件&#xff0c;生成属于您自己的个性化头像。 介绍 您可能感兴趣的功能&#xff1a; 可视化组件配置栏随机生成头像重做/撤消国际化批量生成多个头像 在线体验 Vue 彩色头像https://www.shserve.cn/…

PHP 头像上传到mysql数据库

准备环境 window2008 phpStudy Mysql数据库 mysql数据库 第一步&#xff1a;用户登录成功 该用户已经存在数据库 进入个人中心&#xff0c;查找到数据库中的个人信息 进行图片上次 <?php include "../mysqlcon/dblink.php"; //导入数据库 ?><html> …

用php做论坛头像代码,详细介绍PHP针对多用户实现头像更换代码示例

一个网站&#xff0c;其实说白了就是某几个特定功能的组合&#xff0c;而更换用户头像就在这些功能之中。今天就来做个测试&#xff0c;针对不同的用户&#xff0c;实现头像上传功能。 成品图 思路针对不同的用户上传头像&#xff0c;我们要为每一个已登录的用户创建一个文件夹…

教你用go freetype根据用户昵称生成头像

最近需要为用户服务添加一些新功能&#xff0c;其中就包括在注册时根据用户昵称生成头像这一点。 由于用户服务是用golang写的&#xff0c;google来google去都只找到freetype一个比较简单好用的库&#xff0c;其他比如ImageMagicK之类api都过于低层不适合我们这样相对简单的图…

java ajax 更改头像_ajax+node实现头像更改

好久没有更新博客了&#xff0c;这几天在写文件上传的时候遇到了一个新的问题&#xff0c;就是关于ajax实现文件上传的问题 这几天在做一个小的demo&#xff0c;类似于论坛的一个东西&#xff0c;基于jqueryexpressmongo的一个小的案例&#xff0c;在做到关于设置个人头像的时候…

DISCUZ论坛插件h5手机电脑头像上传3.7.1带扩展插件【收集免费分享】

一个支持电脑和手机h5技术头像上传的插件。 说明&#xff1a;本插件h5电脑版和h5手机版为自主全新开发的触屏版头像上传&#xff0c;体验好&#xff0c;性能好&#xff0c;绿色。 主要特点&#xff1a;支持H5电脑版和H5手机版头像上传。 支持鼠标和触屏操作&#xff0c;支持图…

discuz 头像html5上传,discuz更新H5头像上传

越来越多的浏览器可以慢慢的不在支持flash,对应一些discuz论坛的老的版本来说就需要进行升级操作了,接下来吾爱编程为大家介绍一下discuz头像上传flash改为h5上传的方法,有需要的小伙伴可以参考一下: 1、准备工作: 根据自己的网站编码格式下载对应的最新的版本代码,然后解…

DISCUZ 如何为主题帖列表页添加头像,显示发帖者头像

只显示名字的代码 <em style" font-size:14px;"><!--{if $thread[authorid] && $thread[author]}--><a href"home.php?modspace&uid$thread[authorid]" c"1"{if $groupcolor[$thread[authorid]]} style"color…

Roop:单图离线版软件包及使用方法!

你们要的“单图换脸”离线一键运行版来了。Roop发布几十个小时后&#xff0c;马不停蹄地搞了Colab在线版。其实这东西都挺好的&#xff0c;又快又方便&#xff0c;几乎没有任何硬件要求&#xff0c;点一点就可以搞定了。但是它有一个问题&#xff0c;就是没有“魔法” 就没法使…

被迫在小公司熬了2年,现在我终于进了腾讯测试岗...

其实两年前校招的时候就往腾讯投了一次简历&#xff0c;结果很明显凉了&#xff0c;随后这个理想就被暂时放下了&#xff0c;但是这个种子一直埋在心里&#xff0c;想着总有一天会再次挑战的。 其实这两年除了工作以外&#xff0c;其余时间基本上都在学习&#xff0c;打磨自己…

Oracle-Linux修改字符集

Oracle-修改字符集 连接查询字符集立即关闭数据库并终止所有用户会话开启挂载启用受限会话设置作业队列进程数为0设置 AQ 时间管理进程数为 0打开&#xff08;Open&#xff09;一个已经挂载&#xff08;Mount&#xff09;的数据库修改数据库字符集为AL32UTF8立即关闭数据库并终…

win10/win11更新后没有声音,音频服务未响应

确保扬声器硬件没问题 检查扬声器驱动 打开设备管理器 选择声音、视频和游戏控制器 3.右键属性 4. 更新驱动 5. 从我的电脑 6. 选择最初的驱动

音频服务器未能正常启动,Windows10系统音频服务未启动问题彻底解决方法

近期朋友在电脑的使用过程中遇见了这样一个问题&#xff0c;当他知道win10即将正式推出的时候&#xff0c;忙着给电脑安装测试版的win10系统之后&#xff0c;发现win10系统的音频服务未运行&#xff0c;但是由于win10系统还没有正式发出&#xff0c;导致网上没有多少教程可以查…

电脑音频服务器未修复咋办,音频服务未运行怎么办?win7和win10电脑没声音了恢复方法...

很多人可能都会问电脑没声音了&#xff0c;如何恢复&#xff1f;其实造成这种情况的原因有很多&#xff0c;一般需要一个一个排查。这次&#xff0c;系统迷先跟大家分享其中的一个原因&#xff0c;以及具体的解决方法。 个别小伙伴的笔记本电脑&#xff0c;每次开机总会提示音频…

音频服务器未能正常启动,Win10音频服务未正常运行怎么办?

电脑的音频服务未正常运行&#xff0c;就会导致电脑没有声音&#xff0c;重新安装过驱动&#xff0c;也还是没能解决问题&#xff0c;那么这是什么原因导致的呢&#xff1f;下面小编就来教一下大家Win10音频服务未正常运行的解决方法。 方法/步骤&#xff1a; 1、按 Win R 组合…