通过HttpServletResponse将自定对象或内容以JSON(这边使用了alibaba的fastjson,其依赖包为fastjson-1.2.3.jar)或HTML数据格式返回到终端,通过此工具类将常用流程代码及header头信息都规整好,直接写出结果,具体如下
import java.io.IOException;@b@import java.io.PrintWriter;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@@b@import javax.servlet.http.Cookie;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@@b@import org.apache.commons.lang.StringUtils;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@import com.alibaba.fastjson.JSON;@b@@b@public class OutDataUtil {@b@@b@ /** logger */@b@ private static final Log logger = LogFactory.getLog(OutDataUtil.class);@b@@b@ /**@b@ * 返回响应数据@b@ * @b@ * @param httpServletResponse@b@ * http响应@b@ * @param data@b@ * 数据类@b@ */@b@ public static void outData(HttpServletResponse httpServletResponse,@b@ Object data) {@b@ outData(httpServletResponse, JSON.toJSONString(data));@b@ }@b@@b@ /**@b@ * 返回响应数据@b@ * @b@ * @param httpServletResponse@b@ * http响应@b@ * @param jsonString@b@ * json字符串@b@ */@b@ public static void outData(HttpServletResponse httpServletResponse,@b@ String jsonString) {@b@ try {@b@ httpServletResponse.setCharacterEncoding("utf-8");@b@ httpServletResponse.setHeader("Content-type",@b@ "text/html;charset=utf-8");@b@ PrintWriter writer = httpServletResponse.getWriter();@b@ if (StrUtil.isNotEmpty(jsonString)) {@b@ httpServletResponse.setHeader("Content-Length",@b@ String.valueOf(jsonString.getBytes("utf-8").length));@b@ }@b@ writer.print(jsonString);@b@ logger.info("outData success--------------------:"+jsonString);@b@ httpServletResponse.flushBuffer();@b@ // 无需flush和close,Tomcat会帮我处理@b@ } catch (IOException e) {@b@ logger.error("outData error------------------:", e);@b@ }@b@ }@b@@b@ /** 输出web(H5+PC)请求结果 */@b@ public static void outWebData(HttpServletRequest httpRequest,@b@ HttpServletResponse httpServletResponse, String responseData,@b@ Map<String, String> cookieMap, List<String> toBeDeletedCookies) {@b@ try {@b@ httpServletResponse.setCharacterEncoding("utf-8");@b@ httpServletResponse.setHeader("Content-type",@b@ "text/html;charset=utf-8");@b@@b@ if (cookieMap != null && !cookieMap.isEmpty()) {@b@ Set<Entry<String, String>> entrySet = cookieMap.entrySet();@b@ for (Entry<String, String> entry : entrySet) {@b@ Cookie cookie = new Cookie(entry.getKey(), entry.getValue());@b@ cookie.setMaxAge(60 * 15);@b@ cookie.setSecure(true);@b@ cookie.setPath("/");@b@ httpServletResponse.addCookie(cookie);@b@ }@b@ }@b@ if (toBeDeletedCookies != null && !toBeDeletedCookies.isEmpty()) {@b@ try {@b@ Cookie[] cookieArray = httpRequest.getCookies();@b@ for (Cookie cookie : cookieArray) {@b@ for (String cookeName : toBeDeletedCookies)@b@ if (StringUtils.equals(cookie.getName(), cookeName)) {@b@ cookie.setMaxAge(0);@b@ }@b@ }@b@ } catch (Exception e) {@b@ logger.error("outWebData删除cookie失败", e);@b@ }@b@ }@b@@b@ PrintWriter writer = httpServletResponse.getWriter();@b@ if (StrUtil.isNotEmpty(responseData)) {@b@ httpServletResponse.setHeader("Content-Length",@b@ String.valueOf(responseData.getBytes("utf-8").length));@b@ }@b@ writer.print(responseData);@b@ httpServletResponse.flushBuffer();@b@ } catch (IOException e) {@b@ logger.error("返回数据结果IO异常", e);@b@ }@b@ }@b@@b@}