// 改
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(“update方法被调用”);
HttpSession session=request.getSession();
// 获取数据
int id=(int)session.getAttribute(“id”);
String username=null;
String password=null;
String sex=null;
Date birthday=null;
String address=null;
String saveFileName=null;
String picturePath=null;
IUserService iUserService=new UserServiceImpl();
// 得到表单是否以enctype=”multipart/form-data”方式提交
boolean isMulti=ServletFileUpload.isMultipartContent(request);
if (isMulti) {
// 通过FileItemFactory得到文件上传的对象
FileItemFactory fif=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(fif);
try {
List<FileItem> items=upload.parseRequest(request);
for (FileItem item : items) {
// 判断是否是普通表单控件,或者是文件上传表单控件
boolean isForm=item.isFormField();
if (isForm) {// 是普通表单控件
String name=item.getFieldName();
if (“sex”.equals(name)) {
sex=item.getString(“utf-8”);
System.out.println(sex);
}
if (“username”.equals(name)) {
username=item.getString(“utf-8”);
System.out.println(username);
}
if (“password”.equals(name)) {
password=item.getString(“utf-8”);
System.out.println(password);
}
if (“birthday”.equals(name)) {
String birthdayStr=item.getString(“utf-8”);
SimpleDateFormat sdf=new SimpleDateFormat(
“yyyy-MM-dd”);
try {
birthday=sdf.parse(birthdayStr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(birthday);
}
if (“address”.equals(name)) {
address=item.getString(“utf-8”);
System.out.println(address);
}
if (“picturePath”.equals(name)) {
picturePath=item.getString(“utf-8″);
System.out.println(picturePath);
}
} else {// 是文件上传表单控件
// 得到文件名 xxx.jpg
picturePath=item.getName();
if (picturePath !=””) {// 有选择要上传的图片
// 得到文件名的扩展名:.jpg
String extendedName=picturePath.substring(
picturePath.lastIndexOf(“.”),// 截取从最后一个’.’到字符串结束的子串。
picturePath.length());
// 把文件名称重命名为全球唯一的文件名
String uniqueName=UUID.randomUUID().toString();
saveFileName=uniqueName + extendedName;// 拼接路径名
// 得到上传到服务器上的文件路径
// C:\\apache-tomcat-7.0.47\\webapps\\CommonhelloWorldServlet\?d\\xx.jpg
String uploadFilePath=request.getSession()
.getServletContext().getRealPath(“upload/”);
File saveFile=new File(uploadFilePath,
saveFileName);
// 把保存的文件写出到服务器硬盘上
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
// 3、调用逻辑层 API
// 根据id查询用户并获取其之前的图片
User user=iUserService.getUserById(id);
String oldPic=user.getPicturePath();
String oldPicPath=uploadFilePath + “\” + oldPic;
File oldPicTodelete=new File(oldPicPath);
oldPicTodelete.delete();// 删除旧图片
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileUploadException e) {
e.printStackTrace();
}
}
System.out.println(id + “\t” + username + “\t” + password + “\t” + sex
+ “\t” + address + “\t” + picturePath + “\t” + birthday);
// 改
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(“update方法被调用”);
HttpSession session=request.getSession();
// 获取数据
int id=(int)session.getAttribute(“id”);
String username=null;
String password=null;
String sex=null;
Date birthday=null;
String address=null;
String saveFileName=null;
String picturePath=null;
IUserService iUserService=new UserServiceImpl();
// 得到表单是否以enctype=”multipart/form-data”方式提交
boolean isMulti=ServletFileUpload.isMultipartContent(request);
if (isMulti) {
// 通过FileItemFactory得到文件上传的对象
FileItemFactory fif=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(fif);
try {
List<FileItem> items=upload.parseRequest(request);
for (FileItem item : items) {
// 判断是否是普通表单控件,或者是文件上传表单控件
boolean isForm=item.isFormField();
if (isForm) {// 是普通表单控件
String name=item.getFieldName();
if (“sex”.equals(name)) {
sex=item.getString(“utf-8”);
System.out.println(sex);
}
if (“username”.equals(name)) {
username=item.getString(“utf-8”);
System.out.println(username);
}
if (“password”.equals(name)) {
password=item.getString(“utf-8”);
System.out.println(password);
}
if (“birthday”.equals(name)) {
String birthdayStr=item.getString(“utf-8”);
SimpleDateFormat sdf=new SimpleDateFormat(
“yyyy-MM-dd”);
try {
birthday=sdf.parse(birthdayStr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(birthday);
}
if (“address”.equals(name)) {
address=item.getString(“utf-8”);
System.out.println(address);
}
if (“picturePath”.equals(name)) {
picturePath=item.getString(“utf-8″);
System.out.println(picturePath);
}
} else {// 是文件上传表单控件
// 得到文件名 xxx.jpg
picturePath=item.getName();
if (picturePath !=””) {// 有选择要上传的图片
// 得到文件名的扩展名:.jpg
String extendedName=picturePath.substring(
picturePath.lastIndexOf(“.”),// 截取从最后一个’.’到字符串结束的子串。
picturePath.length());
// 把文件名称重命名为全球唯一的文件名
String uniqueName=UUID.randomUUID().toString();
saveFileName=uniqueName + extendedName;// 拼接路径名
// 得到上传到服务器上的文件路径
// C:\\apache-tomcat-7.0.47\\webapps\\CommonhelloWorldServlet\?d\\xx.jpg
String uploadFilePath=request.getSession()
.getServletContext().getRealPath(“upload/”);
File saveFile=new File(uploadFilePath,
saveFileName);
// 把保存的文件写出到服务器硬盘上
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
// 3、调用逻辑层 API
// 根据id查询用户并获取其之前的图片
User user=iUserService.getUserById(id);
String oldPic=user.getPicturePath();
String oldPicPath=uploadFilePath + “\” + oldPic;
File oldPicTodelete=new File(oldPicPath);
oldPicTodelete.delete();// 删除旧图片
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileUploadException e) {
e.printStackTrace();
}
}
System.out.println(id + “\t” + username + “\t” + password + “\t” + sex
+ “\t” + address + “\t” + picturePath + “\t” + birthday);
// 2、封装数据
User user=new User(id, username, password, sex, birthday, address,
saveFileName);
if (iUserService.update(user) > 0) {
System.out.println(“修改数据成功!”);
List<User> users=new ArrayList<User>();
users=iUserService.listAll();
session.setAttribute(“users”, users);
// 4、控制跳转
response.sendRedirect(“UserServlet?action=getPage”);
} else {
System.out.println(“修改数据失败!”);
PrintWriter out=response.getWriter();
out.print(“<script type=’text/javascript’>”);
out.print(“alert(‘修改数据失败!请重试!’);”);
out.print(“</script>”);
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。