赞
踩
package com.game.common.http.base;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.alibaba.fastjson.JSONObject;
/**
* 公共的action
*
* @author administrator
*
*/
public abstract class BaseActionServlet {
/**
* 获取Ip
*
* @return
*/
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 回传参数
*
* @param response
* @param code
* @param message
*/
public static void responseCode(HttpServletResponse response, String code, String message) {
try {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
json.put("code", code);
json.put("message", message);
out.print(json.toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回传参数
*
* @param response
* @param code
* @param message
*/
public static void responseCode(HttpServletResponse response, int code, String message) {
try {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
json.put("code", code);
json.put("message", message);
out.print(json.toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回传参数
*
* @param response
* @param code
* @param message
*/
public static void responseCode(HttpServletResponse response, Map<String, String> params, int code,
String message) {
try {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
json.put("code", code);
json.put("message", message);
if (null != params) {
params.forEach((k, v) -> {
json.put(k, v);
});
}
out.print(json.toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回传参数
*
* @param resp
* 回传类
* @param data
* 回传数据
* @throws IOException
*/
public static void responseData(HttpServletResponse response, String data) {
try {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(data);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回传参数
*
* @param resp
* @param errorCode
* 错误码
* @throws IOException
*/
public static void responseData(HttpServletResponse response, int errorCode) {
JSONObject json = new JSONObject();
json.put("Result", 0);
json.put("ErrorCode", errorCode);
responseData(response, json.toString());
}
/**
* 回传参数
*
* @param resp
* @param code
* 状态码
* @throws IOException
*/
public static void responseCode(HttpServletResponse response, int code) {
// JSONObject json = new JSONObject();
// json.put("code", code);
String str="{\"code\":"+code+"}";
responseData(response, str);
}
/**
* 回传参数
*
* @param resp
* @param errorCode
* 错误码
* @throws IOException
*/
public static void responseDataSuccess(HttpServletResponse response) {
JSONObject json = new JSONObject();
json.put("Result", 1);
json.put("ErrorCode", 1);
responseData(response, json.toString());
}
/**
* 回传参数,data参数格式key1,value1,key2,value2,key3,value3.....,数据会自动封装成json
*
* @param resp
* @param data
* 格式key1,value1,key2,value2,key3,value3.....
* @throws IOException
*/
public static void responseDatas(HttpServletResponse resp, String... data) {
JSONObject json = new JSONObject();
if (null != data && data.length > 0) {
for (int i = 0; i < data.length; i = i + 2) {
json.put(data[i], data[i + 1]);
}
}
responseData(resp, json.toString());
}
/**
* 回传参数
*
* @param resp
* 回传类
* @param data
* json回传数据
* @throws IOException
*/
public static void responseData(HttpServletResponse response, JSONObject data) {
String str="";
if(null!=data){
str=data.toString();
}
responseData(response,str);
}
/**
* 将参数字符串转换为Map<paramKey,paramValue> eg.
* xxKey=xxValue&yyKey=yyValue&zzKey=zzValue -->
* Map{xxKey=xxValue,yyKey=yyValue,zzKey=zzValue}
*
* @param request
* 要转换的字符串
* @param bools
* 是否第一次读取参数(true 非第一读取参数返回现有paramMap , 空值或false 重新读取paramMap)
* @return Map
* @throws UnsupportedEncodingException
* @throws FileUploadException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map<String, String> getParameters(HttpServletRequest request) throws UnsupportedEncodingException {
// 参数Map
Map<String, String[]> properties = request.getParameterMap();
// 返回值Map
Map<String, String> returnMap = new HashMap<String, String>();
Iterator<?> entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = null;
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if (null == valueObj) {
} else if (valueObj instanceof String[]) {
String[] values = (String[]) valueObj;
for (int i = 0; i < values.length; i++) {
value = values[i] + ",";
}
value = value.substring(0, value.length() - 1);
} else {
value = valueObj.toString().trim();
}
returnMap.put(name, value);
}
return returnMap;
}
/**
* 将参数字符串转换为Map<paramKey,paramValue> eg.
* xxKey=xxValue&yyKey=yyValue&zzKey=zzValue -->
* Map{xxKey=xxValue,yyKey=yyValue,zzKey=zzValue}
*
* (处理multipart/form-data类型的表单请求)
* @param request
* 要转换的字符串
* @param bools
* 是否第一次读取参数(true 非第一读取参数返回现有paramMap , 空值或false 重新读取paramMap)
* @return Map
* @throws UnsupportedEncodingException
* @throws FileUploadException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map<String, String> getParametersForMultipartFormData(HttpServletRequest request)
throws UnsupportedEncodingException, FileUploadException {
Map<String, String> returnMap = new HashMap<String, String>();
// 返回值Map
// 文件上传的三部曲
// 创建工厂
DiskFileItemFactory factoy = new DiskFileItemFactory();
// 创建解析器
ServletFileUpload sfu = new ServletFileUpload(factoy);
// 设置上传文件的大小
sfu.setFileSizeMax(20 * 1024);
// 解析request
List<FileItem> list = sfu.parseRequest(request);
// 只处理非文件内容
for (FileItem fi : list) {
if (fi.isFormField()) {//只要普通字段,不要文件
returnMap.put(fi.getFieldName(), fi.getString());
}
}
return returnMap;
}
/**
* 获取参数
* @param request
* @return
* @throws UnsupportedEncodingException
* @throws FileUploadException
*/
public static Map<String, String> getParams(HttpServletRequest request) throws UnsupportedEncodingException, FileUploadException{
Map<String,String> params=getParameters(request);
if(params.size()>0){
return params;
}
return getParametersForMultipartFormData(request);
}
}
方法:
public static Map<String, String> getParameters(HttpServletRequest request);
解释:从request.getParameterMap()里获取请求参数
方法:
public static Map<String, String> getParametersForMultipartFormData(HttpServletRequest request);
解释:通过上传解析器的形式发送请求参数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。