package com.hustner.controllers;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.hustner.entity.User;
import com.hustner.service.UserService;
public class UserAction {
private String name;
private int sex;
private String phone;
private File header;
private String headerFileName;
private UserService userService;
private Random random;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public File getHeader() {
return header;
}
public void setHeader(File header) {
this.header = header;
}
public String getHeaderFileName() {
return headerFileName;
}
public void setHeaderFileName(String headerFileName) {
this.headerFileName = headerFileName;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String register(){
String ext = headerFileName.substring(headerFileName.lastIndexOf("."));
if(random==null)
random = new Random(System.currentTimeMillis());
String fname=random.nextDouble()+"";
fname=fname.substring(fname.indexOf(".")+1)+ext;
//保存路径
String path=ServletActionContext.getRequest().getRealPath("")+"\\images\\header\\"+fname;
File destFile = new File(path);
try {
FileUtils.copyFile(header, destFile);
User user = new User(name, sex, phone, fname);
if(userService.addUser(user)==null)
throw new Exception();
ServletActionContext.getRequest().getSession(true).setAttribute("user", user);
return "success";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ServletActionContext.getRequest().setAttribute("msg", "保存头像图片出错");
}catch(Exception e){
e.printStackTrace();
ServletActionContext.getRequest().setAttribute("msg", "增加用户信息");
}
return "input";
}
public String reg(){
///获取到uuuuuu.jpg中的.jpg
String end=headerFileName.substring(headerFileName.indexOf("."));
if(random==null)
random=new Random(System.currentTimeMillis());
String fname=random.nextDouble()+"";
//保存在数据库的路径
fname=fname.substring(fname.lastIndexOf(".")+1)+end;
//保存图片的路径
String path=ServletActionContext.getServletContext().getRealPath("")+"\\images\\header\\"+fname;
File file=new File(path);
try {
FileUtils.copyFile(header, file);
User user=new User(name, sex, phone, fname);
if(userService.addUser(user)==null){
throw new Exception();
}
ServletActionContext.getRequest().getSession(true).setAttribute("user", user);
return "success";
} catch (IOException e) {
e.printStackTrace();
ServletActionContext.getRequest().setAttribute("msg", "保存头像图片出错");
} catch (Exception e) {
e.printStackTrace();
ServletActionContext.getRequest().setAttribute("msg", "增加用户出错");
}
/* System.out.println(path);
System.out.println(fname);
System.out.println(end);*/
return "input";
}
public String selectuser(){
System.out.println("执行到了么");
Map<String, Object> request=(Map<String, Object>)ServletActionContext.getContext().get("request");
List<User> users=userService.selectallUser();
System.out.println(users.toString());
if(users==null){
request.put("msg", "加载过程出错");
}
request.put("users", users);
request.put("count", users.size());
return "success";
}
public String selectuser2 () throws IOException{
System.out.println("执行到了么");
Map<String, Object> request=(Map<String, Object>)ServletActionContext.getContext().get("request");
HttpServletResponse response = ServletActionContext.getResponse();
List<User> users=userService.selectallUser();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
//创建json的对象
JSONObject json=new JSONObject();
//把对象users累积到key中。也就是相当于在后面添加功能。
json.accumulate("users", users);
PrintWriter out = response.getWriter();
//out.println(array.toString());
//把json返回给页面
out.println(json.toString());
for(User user:users){
System.out.println(user.toString());
}
System.out.println(json.toString());
//out.println(array.toString()); //输出集合的属性(List,Map)
//out.println(json.toString()); //输出一个对象的属性
out.flush();
out.close();
return "success";
}
}