package web;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import dao.FriendDAO;
import entity.Friend;
import entity.FriendPic;
public class AllServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI();
String path = uri.substring(
uri.lastIndexOf("/"),uri.lastIndexOf("."));
HttpSession session= request.getSession();
if(path.equals("/add")){
String code= request.getParameter("code");
String number2 =(String) session.getAttribute("number");
if(code.equals(number2)){
// Friend friend = new Friend();
String username = request.getParameter("username");
String name = request.getParameter("name");
String pass = request.getParameter("password");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String phone = request.getParameter("phone");
try {
FriendDAO fd = new FriendDAO();
Friend friend = fd.findByUsername(username);
System.out.println(friend);
if(friend!=null){
request.setAttribute("regist_error",
"用户名被占用");
request.getRequestDispatcher("regist.jsp")
.forward(request, response);
}else{
Friend f=new Friend();
// System.out.println(friend+"@@@@@@");
f.setUsername(username);
f.setName(name);
f.setPassword(pass);
f.setAge(Integer.parseInt(age));
f.setSex(sex);
f.setPhone(phone);
fd.addFriend(f);
response.sendRedirect("login.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
String s = "验证码错误";
request.setAttribute("ss", s);
request.getRequestDispatcher("regist.jsp").forward(request,response);
return;
}
}
if(path.equals("/list")){
FriendDAO fd = new FriendDAO();
List<Friend> list = fd.listFriend();
request.setAttribute("friend",list);
RequestDispatcher rd=request
.getRequestDispatcher("listFriend.jsp");
rd.forward(request, response);
}
if(path.equals("/login")){
String username = request.getParameter("username");
String pwd = request.getParameter("password");
FriendDAO fd = new FriendDAO();
try {
Friend friend = fd.findByUsername(username);
if(friend != null && friend.getPassword().equals(pwd)){
//登录成功
session =
request.getSession();
session.setAttribute("friend", friend);
response.sendRedirect("list.do");
}else{
//登录失败
request.setAttribute("login_error",
"username or pass is erro");
request.getRequestDispatcher("login.jsp")
.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if(path.equals("/Detail")){
String username = request.getParameter("username");
FriendDAO dao = new FriendDAO();
Friend friend = null;
FriendPic pic = null;
try {
friend = dao.findByUsername(username);
List<FriendPic> lfp = dao.findPic(friend.getId());
for(int i=0;i<lfp.size();i++){
pic=lfp.get(i);
friend.setFp(pic);
System.out.println(pic.getPicName());
}
session.setAttribute("listf",lfp);
//System.out.println(friend.getId());
request.setAttribute("oneFriend",friend);
//System.out.println(friend.getFp());
request.getRequestDispatcher("friendDetail.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
if(path.equals("/upload")){
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(dfif);
try {
List items = sfu.parseRequest(request);
for(int i=0;i<items.size();i++){
FileItem item =(FileItem)items.get(i);
if(item.isFormField()){
String filedName = item.getFieldName();
String value = item.getString();
// System.out.print(filedName+value);
}else{
String filename1 = item.getName();
System.out.println(filename1);
String filename = filename1.substring(filename1.indexOf("\\")+1);
//System.out.println(filename);
//选择一个存放文件的目录 该目录服务器上的某个目录
//getRealPath("upload")获得服务器上的某个目录的绝对路径
String path1 = getServletContext().getRealPath("upload");
Friend ff = ((Friend)session.getAttribute("friend"));
String mname = ff.getUsername();
File mfile = new File(path1+"/"+mname);
mfile.mkdir();
File file = new File(mfile+"/"+filename);
item.write(file);
FriendPic fp = new FriendPic();
FriendDAO fd = new FriendDAO();
fp.setFiendId(ff.getId());
fp.setPicName(filename);
ff.setFp(fp);
fd.addPic(fp);
request.setAttribute("oneFriend",ff);
request.getRequestDispatcher("friendDetail.jsp").forward(request,response);
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(path.equals("/check")){
response.setContentType("image/jpeg");
// 图片的内存映像
BufferedImage image =
new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
Random r = new Random();
// 获得画笔对象
Graphics g = image.getGraphics();
g.setColor(
new Color(r.nextInt(255),
r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, 60, 20);
g.setColor(new Color(0,0,0));
String number = String.valueOf(
r.nextInt(99999));
session =
request.getSession();
session.setAttribute("number", number);
g.drawString(number, 5, 15);
//加一些干扰线
g.drawLine(r.nextInt(60), r.nextInt(20),
r.nextInt(60), r.nextInt(20));
g.drawLine(r.nextInt(60), r.nextInt(20),
r.nextInt(60), r.nextInt(20));
g.drawLine(r.nextInt(60), r.nextInt(20),
r.nextInt(60), r.nextInt(20));
g.drawLine(r.nextInt(60), r.nextInt(20),
r.nextInt(60), r.nextInt(20));
// 压缩成jpeg格式
OutputStream os = response.getOutputStream();
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(os);
// 把BufferedImage对象中的图像信息编码后
// 向创建该对象(encoder)时指定的输出流输出
encoder.encode(image);
}
}
}