package com.happy.Until; import com.happy.dto.IPage; import net.sf.json.JSONObject; import org.apache.poi.ss.usermodel.Workbook; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.io.PrintWriter; import java.util.List; public class ResponseUtil { public static void write(Object o, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println(o.toString()); out.flush(); out.close(); } public static void write(String o, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println(o.toString()); out.flush(); out.close(); } public static void writeJsonPageData(HttpServletResponse respone, List listPage, int page, int rows, int total) { respone.setContentType("application/json;charset=utf-8"); JSONObject resultJson = new JSONObject(); if (listPage == null || listPage.size() == 0) { resultJson.put("rows", ""); resultJson.put("total", 0); } else { resultJson.put("code", 200); resultJson.put("rows", rows); // int total = listAll.size(); resultJson.put("total", total);// 总记录数 int totalPage = total % rows == 0 ? (total / rows) : (total / rows) + 1;// 总页数 resultJson.put("totalPage", totalPage); resultJson.put("currentPage", page);// 当前页 resultJson.put("numPerPage", rows);// 每页数 resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页 resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页 resultJson.put("hasPreviousPage", true);// 有上一页 resultJson.put("hasNextPage", true);// 有下一页 resultJson.put("firstPage", true);// 首页 resultJson.put("lastPage", true);// 尾页 resultJson.put("listPage", listPage); } PrintWriter out; try { out = respone.getWriter(); out.print(resultJson.toString()); out.flush(); out.close(); } catch (Exception e) { // //System.out.println("Comm_Util_writeJson---->" + e); } } public static void writeJsonIPage(HttpServletResponse respone, IPage iPage) { respone.setContentType("application/json;charset=utf-8"); JSONObject resultJson = new JSONObject(); if (iPage.getPageList() == null || iPage.getPageList().size() == 0) { resultJson.put("rows", ""); resultJson.put("total", 0); } else { resultJson.put("code", 200); resultJson.put("rows", iPage.getRows()); // int total = listAll.size(); resultJson.put("total", iPage.getTotal());// 总记录数 int totalPage = iPage.getTotal() % iPage.getRows() == 0 ? (iPage.getTotal() / iPage.getRows()) : (iPage.getTotal() / iPage.getRows()) + 1;// 总页数 resultJson.put("totalPage", totalPage); resultJson.put("currentPage", iPage.getPage());// 当前页 resultJson.put("numPerPage", iPage.getRows());// 每页数 resultJson.put("nextPage", totalPage - iPage.getPage() == 0 ? iPage.getPage() : iPage.getPage() + 1);// 下一页 resultJson.put("previousPage", iPage.getPage() - 0 == 1 ? iPage.getPage() : iPage.getPage() - 1);// 上一页 resultJson.put("hasPreviousPage", true);// 有上一页 resultJson.put("hasNextPage", true);// 有下一页 resultJson.put("firstPage", true);// 首页 resultJson.put("lastPage", true);// 尾页 resultJson.put("pageList",iPage.getPageList()); // 分页数据 } PrintWriter out; try { out = respone.getWriter(); out.print(resultJson.toString()); out.flush(); out.close(); } catch (Exception e) { // //System.out.println("Comm_Util_writeJson---->" + e); } } public static void writeJson(HttpServletResponse respone, String result) { respone.setContentType("application/json;charset=utf-8"); PrintWriter out; try { out = respone.getWriter(); out.print(result); out.flush(); out.close(); } catch (Exception e) { // //System.out.println("Comm_Util_writeJson---->" + e); } } public static void export(HttpServletResponse response, Workbook wb, String fileName) throws Exception { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1")); response.setContentType("application/ynd.ms-excel;charset=UTF-8"); OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } }