ResponseUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.happy.Until;
  2. import com.happy.dto.IPage;
  3. import net.sf.json.JSONObject;
  4. import org.apache.poi.ss.usermodel.Workbook;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import java.util.List;
  9. public class ResponseUtil {
  10. public static void write(Object o, HttpServletResponse response)
  11. throws Exception {
  12. response.setContentType("text/html;charset=utf-8");
  13. PrintWriter out = response.getWriter();
  14. out.println(o.toString());
  15. out.flush();
  16. out.close();
  17. }
  18. public static void write(String o, HttpServletResponse response)
  19. throws Exception {
  20. response.setContentType("text/html;charset=utf-8");
  21. PrintWriter out = response.getWriter();
  22. out.println(o.toString());
  23. out.flush();
  24. out.close();
  25. }
  26. public static void writeJsonPageData(HttpServletResponse respone, List listPage, int page, int rows, int total) {
  27. respone.setContentType("application/json;charset=utf-8");
  28. JSONObject resultJson = new JSONObject();
  29. if (listPage == null || listPage.size() == 0) {
  30. resultJson.put("rows", "");
  31. resultJson.put("total", 0);
  32. } else {
  33. resultJson.put("code", 200);
  34. resultJson.put("rows", rows);
  35. // int total = listAll.size();
  36. resultJson.put("total", total);// 总记录数
  37. int totalPage = total % rows == 0 ? (total / rows) : (total / rows) + 1;// 总页数
  38. resultJson.put("totalPage", totalPage);
  39. resultJson.put("currentPage", page);// 当前页
  40. resultJson.put("numPerPage", rows);// 每页数
  41. resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
  42. resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
  43. resultJson.put("hasPreviousPage", true);// 有上一页
  44. resultJson.put("hasNextPage", true);// 有下一页
  45. resultJson.put("firstPage", true);// 首页
  46. resultJson.put("lastPage", true);// 尾页
  47. resultJson.put("listPage", listPage);
  48. }
  49. PrintWriter out;
  50. try {
  51. out = respone.getWriter();
  52. out.print(resultJson.toString());
  53. out.flush();
  54. out.close();
  55. } catch (Exception e) {
  56. // //System.out.println("Comm_Util_writeJson---->" + e);
  57. }
  58. }
  59. public static void writeJsonIPage(HttpServletResponse respone, IPage iPage) {
  60. respone.setContentType("application/json;charset=utf-8");
  61. JSONObject resultJson = new JSONObject();
  62. if (iPage.getPageList() == null || iPage.getPageList().size() == 0) {
  63. resultJson.put("rows", "");
  64. resultJson.put("total", 0);
  65. } else {
  66. resultJson.put("code", 200);
  67. resultJson.put("rows", iPage.getRows());
  68. // int total = listAll.size();
  69. resultJson.put("total", iPage.getTotal());// 总记录数
  70. int totalPage = iPage.getTotal() % iPage.getRows() == 0 ? (iPage.getTotal() / iPage.getRows()) : (iPage.getTotal() / iPage.getRows()) + 1;// 总页数
  71. resultJson.put("totalPage", totalPage);
  72. resultJson.put("currentPage", iPage.getPage());// 当前页
  73. resultJson.put("numPerPage", iPage.getRows());// 每页数
  74. resultJson.put("nextPage", totalPage - iPage.getPage() == 0 ? iPage.getPage() : iPage.getPage() + 1);// 下一页
  75. resultJson.put("previousPage", iPage.getPage() - 0 == 1 ? iPage.getPage() : iPage.getPage() - 1);// 上一页
  76. resultJson.put("hasPreviousPage", true);// 有上一页
  77. resultJson.put("hasNextPage", true);// 有下一页
  78. resultJson.put("firstPage", true);// 首页
  79. resultJson.put("lastPage", true);// 尾页
  80. resultJson.put("pageList",iPage.getPageList()); // 分页数据
  81. }
  82. PrintWriter out;
  83. try {
  84. out = respone.getWriter();
  85. out.print(resultJson.toString());
  86. out.flush();
  87. out.close();
  88. } catch (Exception e) {
  89. // //System.out.println("Comm_Util_writeJson---->" + e);
  90. }
  91. }
  92. public static void writeJson(HttpServletResponse respone, String result) {
  93. respone.setContentType("application/json;charset=utf-8");
  94. PrintWriter out;
  95. try {
  96. out = respone.getWriter();
  97. out.print(result);
  98. out.flush();
  99. out.close();
  100. } catch (Exception e) {
  101. // //System.out.println("Comm_Util_writeJson---->" + e);
  102. }
  103. }
  104. public static void export(HttpServletResponse response, Workbook wb,
  105. String fileName) throws Exception {
  106. response.setHeader("Content-Disposition", "attachment;filename="
  107. + new String(fileName.getBytes("utf-8"), "iso8859-1"));
  108. response.setContentType("application/ynd.ms-excel;charset=UTF-8");
  109. OutputStream out = response.getOutputStream();
  110. wb.write(out);
  111. out.flush();
  112. out.close();
  113. }
  114. }