ServletUtil.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.szwl.util;
  2. import com.alibaba.fastjson.JSON;
  3. import org.springframework.http.MediaType;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.InetAddress;
  9. import java.net.URLEncoder;
  10. import java.net.UnknownHostException;
  11. import java.nio.charset.StandardCharsets;
  12. /**
  13. * @author wuhs
  14. * @date 2021-11-01 14:13
  15. */
  16. public class ServletUtil {
  17. /**
  18. * 组装下载文件请求体,防止中文乱码
  19. *
  20. * @param response
  21. * @param fileName
  22. * @return
  23. * @throws UnsupportedEncodingException
  24. */
  25. public static HttpServletResponse buildDownloadHeader(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
  26. response.reset();
  27. response.setCharacterEncoding("UTF-8");
  28. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  29. //防止中文乱码
  30. String percentEncodedFileName = percentEncode(fileName);
  31. StringBuilder contentDispositionValue = new StringBuilder();
  32. contentDispositionValue.append("attachment; filename=")
  33. .append(percentEncodedFileName)
  34. .append(";")
  35. .append("filename*=")
  36. .append("utf-8''")
  37. .append(percentEncodedFileName);
  38. response.setHeader("Content-disposition", contentDispositionValue.toString());
  39. return response;
  40. }
  41. /**
  42. * 百分号编码工具方法
  43. *
  44. * @param s 需要百分号编码的字符串
  45. * @return 百分号编码后的字符串
  46. */
  47. private static String percentEncode(String s) throws UnsupportedEncodingException {
  48. String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
  49. return encode.replaceAll("\\+", "%20");
  50. }
  51. /**
  52. * 以json方式渲染到前端
  53. *
  54. * @param response
  55. * @param value
  56. */
  57. public static void renderJSON(HttpServletResponse response, Object value) throws IOException {
  58. response.setContentType("application/json;charset=UTF-8");
  59. response.getWriter().write(JSON.toJSONString(value));
  60. response.getWriter().flush();
  61. response.getWriter().close();
  62. }
  63. /**
  64. * 获取IP方法
  65. *
  66. * @author ruoyi
  67. */
  68. public static String getIpAddr(HttpServletRequest request) {
  69. if (request == null) {
  70. return "unknown";
  71. }
  72. String ip = request.getHeader("x-forwarded-for");
  73. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  74. ip = request.getHeader("Proxy-Client-IP");
  75. }
  76. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  77. ip = request.getHeader("X-Forwarded-For");
  78. }
  79. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  80. ip = request.getHeader("WL-Proxy-Client-IP");
  81. }
  82. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  83. ip = request.getHeader("X-Real-IP");
  84. }
  85. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  86. ip = request.getRemoteAddr();
  87. }
  88. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
  89. }
  90. public static boolean internalIp(String ip) {
  91. byte[] addr = textToNumericFormatV4(ip);
  92. return internalIp(addr) || "127.0.0.1".equals(ip);
  93. }
  94. private static boolean internalIp(byte[] addr) {
  95. if (addr == null || addr.length < 2) {
  96. return true;
  97. }
  98. final byte b0 = addr[0];
  99. final byte b1 = addr[1];
  100. // 10.x.x.x/8
  101. final byte SECTION_1 = 0x0A;
  102. // 172.16.x.x/12
  103. final byte SECTION_2 = (byte) 0xAC;
  104. final byte SECTION_3 = (byte) 0x10;
  105. final byte SECTION_4 = (byte) 0x1F;
  106. // 192.168.x.x/16
  107. final byte SECTION_5 = (byte) 0xC0;
  108. final byte SECTION_6 = (byte) 0xA8;
  109. switch (b0) {
  110. case SECTION_1:
  111. return true;
  112. case SECTION_2:
  113. if (b1 >= SECTION_3 && b1 <= SECTION_4) {
  114. return true;
  115. }
  116. case SECTION_5:
  117. switch (b1) {
  118. case SECTION_6:
  119. return true;
  120. }
  121. default:
  122. return false;
  123. }
  124. }
  125. /**
  126. * 将IPv4地址转换成字节
  127. *
  128. * @param text IPv4地址
  129. * @return byte 字节
  130. */
  131. public static byte[] textToNumericFormatV4(String text) {
  132. if (text.length() == 0) {
  133. return null;
  134. }
  135. byte[] bytes = new byte[4];
  136. String[] elements = text.split("\\.", -1);
  137. try {
  138. long l;
  139. int i;
  140. switch (elements.length) {
  141. case 1:
  142. l = Long.parseLong(elements[0]);
  143. if ((l < 0L) || (l > 4294967295L)) {
  144. return null;
  145. }
  146. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  147. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  148. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  149. bytes[3] = (byte) (int) (l & 0xFF);
  150. break;
  151. case 2:
  152. l = Integer.parseInt(elements[0]);
  153. if ((l < 0L) || (l > 255L)) {
  154. return null;
  155. }
  156. bytes[0] = (byte) (int) (l & 0xFF);
  157. l = Integer.parseInt(elements[1]);
  158. if ((l < 0L) || (l > 16777215L)) {
  159. return null;
  160. }
  161. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  162. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  163. bytes[3] = (byte) (int) (l & 0xFF);
  164. break;
  165. case 3:
  166. for (i = 0; i < 2; ++i) {
  167. l = Integer.parseInt(elements[i]);
  168. if ((l < 0L) || (l > 255L)) {
  169. return null;
  170. }
  171. bytes[i] = (byte) (int) (l & 0xFF);
  172. }
  173. l = Integer.parseInt(elements[2]);
  174. if ((l < 0L) || (l > 65535L)) {
  175. return null;
  176. }
  177. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  178. bytes[3] = (byte) (int) (l & 0xFF);
  179. break;
  180. case 4:
  181. for (i = 0; i < 4; ++i) {
  182. l = Integer.parseInt(elements[i]);
  183. if ((l < 0L) || (l > 255L)) {
  184. return null;
  185. }
  186. bytes[i] = (byte) (int) (l & 0xFF);
  187. }
  188. break;
  189. default:
  190. return null;
  191. }
  192. } catch (NumberFormatException e) {
  193. return null;
  194. }
  195. return bytes;
  196. }
  197. public static String getHostIp() {
  198. try {
  199. return InetAddress.getLocalHost().getHostAddress();
  200. } catch (UnknownHostException e) {
  201. }
  202. return "127.0.0.1";
  203. }
  204. public static String getHostName() {
  205. try {
  206. return InetAddress.getLocalHost().getHostName();
  207. } catch (UnknownHostException e) {
  208. }
  209. return "未知";
  210. }
  211. }