修改个人资料
修改头像
同步请求,将用户的id,用样式"display;none"偷偷藏起来,一起将id和其他数据一起悄咪咪的提交给servlet处理。
新增两个方法:
// 修改个人资料
@Override
public Boolean update(String id, String nickname, String sex) throws SQLException {
int update = queryRunner.update("update user set nickname=? ,sex =? where id=? ",nickname,sex,id);
if (update > 0) {
return true;
}
return false;
}
// 修改头像
@Override
public boolean ChangePicture(String id, String newFimeName) throws SQLException {
int update = queryRunner.update("update user set picture=? where id=? ",newFimeName,id);
if (update > 0) {
return true;
}
return false;
}
新增两个Dao方法,用于用户的个人信息及头像的修改。
两个新的servlet:
package Servlet;
import Domain.User;
import Service.Impl.UserServiceImpl;
import Service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
@WebServlet(name = "ChangePersonServlet")
public class ChangePersonServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gb2312");
// 设置输出
PrintWriter out = response.getWriter();
// 获得id
String id = request.getParameter("changeID");
String nickname = request.getParameter("nickname");
String sex = request.getParameter("sex");
System.out.println(id+nickname+sex);
// 根据id更改person
UserService userService = new UserServiceImpl();
boolean result = false;
try {
result = userService.update(id, nickname, sex);
} catch (SQLException e) {
e.printStackTrace();
}
if (result){
out.print("");
}else{
out.print("");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package Servlet;
import Domain.User;
import Service.Impl.UserServiceImpl;
import Service.UserService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@WebServlet(name = "ChangePictureServlet")
public class ChangePictureServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gb2312");
// 设置输出
PrintWriter outprint = response.getWriter();
// 根据id找到User对象,用于Oldpic的删除
User user = new User();
UserService userService = new UserServiceImpl();
// 上传文件
// 设置文件目录
String webroot = this.getServletContext().getRealPath("/");
File temppath = new File(webroot + "fileuploadtemp");
String dir = webroot + File.separator + "upload";
File path = new File(webroot + File.separator + "upload");
if (!temppath.exists()) {
temppath.mkdirs();
}
if (!path.exists()) {
path.mkdirs();
}
// 设置文件类型(后期可新增文件类型)
String[] type = new String[]{".jpg", ".png", ".jpeg", ".gif"};
// 创建文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024,
temppath);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(1024 * 1024 * 10);
try {
List fileItems = upload.parseRequest(request);
Iterator it = fileItems.iterator();
String id = null;
// 遍历request file
while (it.hasNext()) {
FileItem fi = it.next();
// 判断该表单为普通表单类型
if (fi.isFormField()) {
// 获得id
id = fi.getString();
user = userService.select(id);
} else {
InputStream in = fi.getInputStream();
String name = fi.getName();//获得文件原名
// 得到文件后缀名
int index = name.lastIndexOf(".");
if (index == -1) {
outprint.print("");
return;
}
String endWith = name.substring(index);
// 判断是否符合类型
boolean TypeExists = Arrays.asList(type).contains(endWith);
if (!TypeExists) {
outprint.print("");
return;
}
String newFimeName = System.currentTimeMillis() + endWith;//新文件名
// 更改数据库picture字段
boolean result = userService.ChangePicture(id, newFimeName);
if (result) {
// 创建上传文件
FileOutputStream out = new FileOutputStream(new File(
dir + "/" + newFimeName));
byte buffer[] = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);//写入大小
}
in.close();
out.close();
fi.delete();
outprint.print("");
//删除原头像文件
//避免空指针
if (!("1.png").equals(user.getPicture())) {
File delete = new File(dir + "/" + user.getPicture());
delete.delete();
}
}
}
}
} catch (FileUploadException e) {
response.getWriter().write(e.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
更改个人资料很简单,先根据id找人,再update更新就行;
再更改头像的时候,本准备修改个人资料和头像一起进行更改,但是由于用户可能只需要更改用户昵称,不选择需要更改头像的时候,再上传文件的时候,java代码会报错,无奈解决不了,只能两个分开进行修改了。
再注册的servlet,可以在picture字段给用户一个默认的头像。
修改个人信息也做了点手脚,在提交前,input的value值就是用户修改前的信息,欸嘿嘿嘿。