package com.fanruan.api.web; import com.fanruan.api.net.NetworkKit; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; /** * @author richie * @version 10.0 * Created by richie on 2019-09-19 * 将内容输入到输出流中 */ public class FlushKit { /** * 输出JSON类型的字符串 * * @param res HTTP响应 * @param jo JSON对象 * @throws Exception 输出出现错误则抛出此异常 */ public static void printAsJSON(HttpServletResponse res, JSONObject jo) throws Exception { printAsString(res, jo.toString()); } /** * 输出JSON类型的字符串 * * @param res HTTP响应 * @param ja JSON数组对象 * @throws Exception 输出出现错误则抛出此异常 */ public static void printAsJSON(HttpServletResponse res, JSONArray ja) throws Exception { printAsString(res, ja.toString()); } /** * 输出字符串,一般来说是JSON格式 * * @param res HTTP响应 * @param jo JSON样式的字符串 * @throws Exception 输出出现错误则抛出此异常 */ public static void printAsString(HttpServletResponse res, String jo) throws Exception { PrintWriter pw = NetworkKit.createPrintWriter(res); pw.print(jo); pw.flush(); pw.close(); } }