ResponseUtil.java 3.5 KB

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