提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.awt.image.BufferedImage; |
|
4 |
import java.io.BufferedReader; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileInputStream; |
|
7 |
import java.io.IOException; |
|
8 |
import java.io.InputStreamReader; |
|
9 |
import java.io.OutputStream; |
|
10 |
import java.io.PrintWriter; |
|
11 |
import java.nio.charset.Charset; |
|
12 |
import java.util.Date; |
|
13 |
|
|
14 |
import javax.imageio.ImageIO; |
|
15 |
import javax.servlet.ServletException; |
|
16 |
import javax.servlet.ServletResponse; |
|
17 |
import javax.servlet.http.HttpServletRequest; |
|
18 |
import javax.servlet.http.HttpServletResponse; |
|
19 |
import javax.servlet.http.HttpSession; |
|
20 |
|
|
21 |
import org.apache.commons.lang3.StringUtils; |
|
22 |
|
|
23 |
import com.mandi.system.persist.Login; |
|
24 |
import com.mandi.system.persist.LoginSide; |
|
25 |
|
|
26 |
/** |
|
27 |
* 封装了servlet相关接口 |
|
28 |
* @author Administrator |
|
29 |
* |
|
30 |
*/ |
|
31 |
public class SessionMethod { |
|
32 |
public static String getloginid(HttpSession session) |
|
33 |
{ |
|
34 |
Login login=(Login)session.getAttribute("login"); |
|
35 |
if(login!=null) |
|
36 |
return login.getUserid(); |
|
37 |
return ""; |
|
38 |
} |
|
39 |
public static Login getlogin(HttpSession session) |
|
40 |
{ |
|
41 |
Login login=(Login)session.getAttribute("login"); |
|
42 |
return login; |
|
43 |
} |
|
44 |
public static boolean setlogin(HttpSession session,Login login) |
|
45 |
{ |
|
46 |
session.setAttribute("login", login); |
|
47 |
session.setAttribute("login_sess_id", login.getId());//login id |
|
48 |
session.setAttribute("login_sess_userid", login.getUserid());//user表 userid |
|
49 |
session.setAttribute("login_sess_companyno", login.getDepartno());//user 表 companyNo |
|
50 |
session.setAttribute("login_sess_username", login.getUsername());//user 表username |
|
51 |
session.setAttribute("login_sess_loginside", login.getLoginside());//暂时没用 |
|
52 |
session.setAttribute("login_sess_companyname", login.getDepartname());//company表 name |
|
53 |
session.setAttribute("login_sess_workername", login.getWorkername());//user表name |
|
54 |
session.setAttribute("login_sess_utype", login.getWorkerno());//admin/worker 判断是否是管理员 |
|
55 |
session.setAttribute("login_sess_fd_utype", login.isFdAdmin());//admin/worker 判断是否是管理员 |
|
56 |
String sc=(new Date().getTime())+""; |
|
57 |
session.setAttribute("login_page_vesion", sc); |
|
58 |
return true; |
|
59 |
} |
|
60 |
public static boolean setGateslogin(HttpSession session,String gatesNo) |
|
61 |
{ |
|
62 |
session.setAttribute("login_sess_gatesNo", gatesNo); |
|
63 |
return true; |
|
64 |
} |
|
65 |
public static String getGateslogin(HttpSession session,String gatesNo) |
|
66 |
{ |
|
67 |
String sessiongatesNo= (String) session.getAttribute("login_sess_gatesNo"); |
|
68 |
return sessiongatesNo; |
|
69 |
} |
|
70 |
public static void writeresp(HttpServletResponse resp,String content) |
|
71 |
{ |
|
72 |
if(resp==null||content==null) |
|
73 |
return; |
|
74 |
PrintWriter w=null; |
|
75 |
try { |
|
76 |
resp.setCharacterEncoding("utf-8"); |
|
77 |
resp.setContentType("application/json; charset=utf-8"); |
|
78 |
w=resp.getWriter(); |
|
79 |
w.write(content); |
|
80 |
} catch (IOException e) { |
|
81 |
e.printStackTrace(); |
|
82 |
} |
|
83 |
} |
|
84 |
public static void writerespstr(HttpServletResponse resp,String content) |
|
85 |
{ |
|
86 |
if(resp==null||content==null) |
|
87 |
return; |
|
88 |
PrintWriter w=null; |
|
89 |
try { |
|
90 |
resp.setCharacterEncoding("utf-8"); |
|
91 |
resp.setContentType("text/html; charset=utf-8"); |
|
92 |
w=resp.getWriter(); |
|
93 |
w.write(content); |
|
94 |
} catch (IOException e) { |
|
95 |
e.printStackTrace(); |
|
96 |
} |
|
97 |
} |
|
98 |
public static void writeImg(HttpServletResponse resp,String filepath) |
|
99 |
{ |
|
100 |
if(resp==null||filepath==null) |
|
101 |
return; |
|
102 |
File f=new File(filepath); |
|
103 |
if(!f.exists()) |
|
104 |
return; |
|
105 |
try { |
|
106 |
BufferedImage img=null; |
|
107 |
img=ImageIO.read(new FileInputStream(new File(filepath))); |
|
108 |
ImageIO.write(img, "jpeg", resp.getOutputStream()); |
|
109 |
} catch (IOException e) { |
|
110 |
e.printStackTrace(); |
|
111 |
} |
|
112 |
} |
|
113 |
public static void writerespstream(HttpServletResponse resp,String content) |
|
114 |
{ |
|
115 |
if(resp==null||content==null) |
|
116 |
return; |
|
117 |
OutputStream os=null; |
|
118 |
try { |
|
119 |
resp.setCharacterEncoding("utf-8"); |
|
120 |
os=resp.getOutputStream(); |
|
121 |
os.write(content.getBytes("utf-8")); |
|
122 |
} catch (IOException e) { |
|
123 |
e.printStackTrace(); |
|
124 |
} |
|
125 |
} |
|
126 |
public static void setlogin(HttpSession session,String id,String username,long depart,LoginSide ls) |
|
127 |
{ |
|
128 |
Login l=new Login(); |
|
129 |
l.setUserid(id); |
|
130 |
l.setDepart(depart); |
|
131 |
l.setUsername(username); |
|
132 |
l.setLoginside(ls); |
|
133 |
session.setAttribute("login", l); |
|
134 |
session.setAttribute("login_sess_id", id); |
|
135 |
session.setAttribute("login_sess_depart", depart); |
|
136 |
session.setAttribute("login_sess_username", username); |
|
137 |
session.setAttribute("login_sess_loginside", ls); |
|
138 |
String sc=(new Date().getTime())+""; |
|
139 |
session.setAttribute("login_page_vesion", sc); |
|
140 |
|
|
141 |
} |
|
142 |
|
|
143 |
public static void forward(ServletResponse resp,HttpServletRequest r,String url) |
|
144 |
{ |
|
145 |
if(resp==null||r==null||url==null) |
|
146 |
return; |
|
147 |
System.out.println(url); |
|
148 |
try { |
|
149 |
HttpServletRequest hr=(HttpServletRequest)r; |
|
150 |
hr.getRequestDispatcher(url).forward(r, resp); |
|
151 |
} catch (IOException e) { |
|
152 |
e.printStackTrace(); |
|
153 |
} catch (ServletException e) { |
|
154 |
e.printStackTrace(); |
|
155 |
} |
|
156 |
} |
|
157 |
public static void sendRedirect(HttpServletResponse resp,HttpServletRequest r,String url) |
|
158 |
{ |
|
159 |
if(resp==null||r==null||url==null) |
|
160 |
return; |
|
161 |
System.out.println(url); |
|
162 |
url=r.getServletContext().getContextPath()+url; |
|
163 |
try { |
|
164 |
resp.sendRedirect(url); |
|
165 |
} catch (IOException e) { |
|
166 |
e.printStackTrace(); |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
public static String postBody(HttpServletRequest r) |
|
171 |
{ |
|
172 |
if(r==null) |
|
173 |
return null; |
|
174 |
StringBuffer sb=new StringBuffer(); |
|
175 |
try { |
|
176 |
BufferedReader reader = new BufferedReader(new InputStreamReader(r.getInputStream(),Charset.forName("utf-8"))); |
|
177 |
String line=null; |
|
178 |
while((line=reader.readLine())!=null) |
|
179 |
{ |
|
180 |
sb.append(line); |
|
181 |
} |
|
182 |
} catch (IOException e) { |
|
183 |
e.printStackTrace(); |
|
184 |
} |
|
185 |
return sb.toString(); |
|
186 |
} |
|
187 |
|
|
188 |
public static boolean checkvalicode(String code,HttpSession session){ |
|
189 |
if(StringUtils.isBlank(code)) |
|
190 |
return false; |
|
191 |
String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); |
|
192 |
if(StringUtils.isNotBlank(scode)){ |
|
193 |
if(code.trim().equals(scode.trim())){ |
|
194 |
return true; |
|
195 |
} |
|
196 |
} |
|
197 |
// int a=KaptchaTextutil.calculateNum(scode); |
|
198 |
// if(code.trim().equals(a+"")) |
|
199 |
// return true; |
|
200 |
return false; |
|
201 |
} |
|
202 |
public static boolean checkvalicodetogetphonecode(String code,HttpSession session){ |
|
203 |
if(StringUtils.isEmpty(code)) |
|
204 |
return false; |
|
205 |
String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); |
|
206 |
if(code.trim().equals(scode.trim())){ |
|
207 |
return true; |
|
208 |
} |
|
209 |
// int a=KaptchaTextutil.calculateNum(scode); |
|
210 |
// if(code.trim().equals(a+"")) |
|
211 |
// { |
|
212 |
// session.removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); |
|
213 |
// return true; |
|
214 |
// } |
|
215 |
return false; |
|
216 |
} |
|
217 |
} |