package com.mandi.common; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.nio.charset.Charset; import java.util.Date; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import com.mandi.system.persist.Login; import com.mandi.system.persist.LoginSide; /** * 封装了servlet相关接口 * @author Administrator * */ public class SessionMethod { public static String getloginid(HttpSession session) { Login login=(Login)session.getAttribute("login"); if(login!=null) return login.getUserid(); return ""; } public static Login getlogin(HttpSession session) { Login login=(Login)session.getAttribute("login"); return login; } public static boolean setlogin(HttpSession session,Login login) { session.setAttribute("login", login); session.setAttribute("login_sess_id", login.getId());//login id session.setAttribute("login_sess_userid", login.getUserid());//user表 userid session.setAttribute("login_sess_companyno", login.getDepartno());//user 表 companyNo session.setAttribute("login_sess_username", login.getUsername());//user 表username session.setAttribute("login_sess_loginside", login.getLoginside());//暂时没用 session.setAttribute("login_sess_companyname", login.getDepartname());//company表 name session.setAttribute("login_sess_workername", login.getWorkername());//user表name session.setAttribute("login_sess_utype", login.getWorkerno());//admin/worker 判断是否是管理员 session.setAttribute("login_sess_fd_utype", login.isFdAdmin());//admin/worker 判断是否是管理员 String sc=(new Date().getTime())+""; session.setAttribute("login_page_vesion", sc); return true; } public static boolean setGateslogin(HttpSession session,String gatesNo) { session.setAttribute("login_sess_gatesNo", gatesNo); return true; } public static String getGateslogin(HttpSession session,String gatesNo) { String sessiongatesNo= (String) session.getAttribute("login_sess_gatesNo"); return sessiongatesNo; } public static void writeresp(HttpServletResponse resp,String content) { if(resp==null||content==null) return; PrintWriter w=null; try { resp.setCharacterEncoding("utf-8"); resp.setContentType("application/json; charset=utf-8"); w=resp.getWriter(); w.write(content); } catch (IOException e) { e.printStackTrace(); } } public static void writerespstr(HttpServletResponse resp,String content) { if(resp==null||content==null) return; PrintWriter w=null; try { resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html; charset=utf-8"); w=resp.getWriter(); w.write(content); } catch (IOException e) { e.printStackTrace(); } } public static void writeImg(HttpServletResponse resp,String filepath) { if(resp==null||filepath==null) return; File f=new File(filepath); if(!f.exists()) return; try { BufferedImage img=null; img=ImageIO.read(new FileInputStream(new File(filepath))); ImageIO.write(img, "jpeg", resp.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } public static void writerespstream(HttpServletResponse resp,String content) { if(resp==null||content==null) return; OutputStream os=null; try { resp.setCharacterEncoding("utf-8"); os=resp.getOutputStream(); os.write(content.getBytes("utf-8")); } catch (IOException e) { e.printStackTrace(); } } public static void setlogin(HttpSession session,String id,String username,long depart,LoginSide ls) { Login l=new Login(); l.setUserid(id); l.setDepart(depart); l.setUsername(username); l.setLoginside(ls); session.setAttribute("login", l); session.setAttribute("login_sess_id", id); session.setAttribute("login_sess_depart", depart); session.setAttribute("login_sess_username", username); session.setAttribute("login_sess_loginside", ls); String sc=(new Date().getTime())+""; session.setAttribute("login_page_vesion", sc); } public static void forward(ServletResponse resp,HttpServletRequest r,String url) { if(resp==null||r==null||url==null) return; System.out.println(url); try { HttpServletRequest hr=(HttpServletRequest)r; hr.getRequestDispatcher(url).forward(r, resp); } catch (IOException e) { e.printStackTrace(); } catch (ServletException e) { e.printStackTrace(); } } public static void sendRedirect(HttpServletResponse resp,HttpServletRequest r,String url) { if(resp==null||r==null||url==null) return; System.out.println(url); url=r.getServletContext().getContextPath()+url; try { resp.sendRedirect(url); } catch (IOException e) { e.printStackTrace(); } } public static String postBody(HttpServletRequest r) { if(r==null) return null; StringBuffer sb=new StringBuffer(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(r.getInputStream(),Charset.forName("utf-8"))); String line=null; while((line=reader.readLine())!=null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } public static boolean checkvalicode(String code,HttpSession session){ if(StringUtils.isBlank(code)) return false; String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if(StringUtils.isNotBlank(scode)){ if(code.trim().equals(scode.trim())){ return true; } } // int a=KaptchaTextutil.calculateNum(scode); // if(code.trim().equals(a+"")) // return true; return false; } public static boolean checkvalicodetogetphonecode(String code,HttpSession session){ if(StringUtils.isEmpty(code)) return false; String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if(code.trim().equals(scode.trim())){ return true; } // int a=KaptchaTextutil.calculateNum(scode); // if(code.trim().equals(a+"")) // { // session.removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); // return true; // } return false; } }