package com.se.nsl.helper;
|
|
import com.alibaba.fastjson.JSON;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
import java.net.InetAddress;
|
import java.net.URLEncoder;
|
import java.net.UnknownHostException;
|
import java.sql.Timestamp;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@Slf4j
|
@SuppressWarnings("ALL")
|
public class WebHelper {
|
public final static String POINT = ".";
|
|
private final static String COMMA = ",";
|
|
private final static String UNKNOWN = "unknown";
|
|
public static boolean isWin() {
|
String osName = System.getProperty("os.name");
|
|
return osName.startsWith("Windows");
|
}
|
|
public static int getCpuCores() {
|
return Runtime.getRuntime().availableProcessors();
|
}
|
|
public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
public static boolean isEmpty(String str) {
|
return null == str || "".equals(str.trim());
|
}
|
|
public static String getGuid() {
|
return UUID.randomUUID().toString();
|
}
|
|
public static String getHostIp() {
|
try {
|
return InetAddress.getLocalHost().getHostAddress();
|
} catch (UnknownHostException e) {
|
//
|
}
|
return "127.0.0.1";
|
}
|
|
public static String getIpAddress(HttpServletRequest request) {
|
String ip = request.getHeader("X-Forwarded-For");
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_FORWARDED");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_VIA");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("REMOTE_ADDR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
if (ip.contains(COMMA)) {
|
return ip.split(",")[0];
|
}
|
|
return ip;
|
}
|
|
public static Timestamp getCurrentTimestamp() {
|
return new Timestamp(System.currentTimeMillis());
|
}
|
|
public static HttpServletRequest getRequest() {
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
return servletRequestAttributes.getRequest();
|
}
|
|
public static HttpServletResponse getResponse() {
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
return servletRequestAttributes.getResponse();
|
}
|
|
public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, Object obj) {
|
res.setStatus(status.value());
|
|
return writeStr2Page(res, JSON.toJSONString(obj));
|
}
|
|
public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, String str) {
|
res.setStatus(status.value());
|
|
Map<String, Object> map = new HashMap(2);
|
map.put("code", status.value() >= 400 ? -1 : 0);
|
map.put("msg", str);
|
|
return writeStr2Page(res, JSON.toJSONString(map));
|
}
|
|
public static boolean writeStr2Page(HttpServletResponse res, HttpStatus status, String str) {
|
res.setStatus(status.value());
|
|
return writeStr2Page(res, str);
|
}
|
|
public static boolean writeStr2Page(HttpServletResponse res, String str) {
|
try {
|
res.setContentType("application/json;charset=UTF-8");
|
res.setHeader("Cache-Control", "no-cache");
|
res.setHeader("Pragma", "No-cache");
|
res.setDateHeader("Expires", 0);
|
|
PrintWriter out = res.getWriter();
|
out.print(str);
|
|
out.flush();
|
out.close();
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
|
return false;
|
}
|
|
public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException {
|
res.setContentType("application/octet-stream");
|
|
if (null == bytes) {
|
res.setStatus(HttpStatus.NOT_FOUND.value());
|
return;
|
}
|
|
OutputStream os = res.getOutputStream();
|
os.write(bytes, 0, bytes.length);
|
os.close();
|
}
|
|
public static void writePng(String filePath, HttpServletResponse res) throws IOException {
|
File file = new File(filePath);
|
if (!file.exists() || file.isDirectory()) {
|
res.setStatus(HttpStatus.NOT_FOUND.value());
|
return;
|
}
|
|
String fileName = URLEncoder.encode(filePath, "UTF-8").replace("+", "%20");
|
res.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
|
res.setCharacterEncoding("UTF-8");
|
res.setContentType("image/png");
|
|
writeFile(filePath, res);
|
}
|
|
public static int getRandomInt(int min, int max) {
|
return new Random().nextInt(max) % (max - min + 1) + min;
|
}
|
|
public static void download(String file, String fileName, HttpServletResponse res) throws Exception {
|
download(file, fileName, false, res);
|
}
|
|
public static void download(String file, String fileName, boolean inline, HttpServletResponse res) throws Exception {
|
if (isEmpty(fileName)) {
|
fileName = YMDHMS.format(new Date());
|
}
|
fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
|
String dispose = inline ? "inline" : "attachment";
|
|
res.setHeader("Content-Disposition", dispose + "; filename*=UTF-8''" + fileName);
|
res.setCharacterEncoding("UTF-8");
|
|
String ext = getExtension(file);
|
String mime = getMime(ext);
|
res.setContentType(mime);
|
|
writeFile(file, res);
|
}
|
|
private static void writeFile(String file, HttpServletResponse res) throws IOException {
|
ServletOutputStream outputStream = res.getOutputStream();
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
int len = 0;
|
byte[] bytes = new byte[1024];
|
while ((len = fileInputStream.read(bytes)) != -1) {
|
outputStream.write(bytes, 0, len);
|
outputStream.flush();
|
}
|
|
fileInputStream.close();
|
outputStream.close();
|
}
|
|
public static String getExtension(String fileName) {
|
if (isEmpty(fileName)) {
|
return "";
|
}
|
|
int idx = fileName.lastIndexOf(POINT);
|
if (idx == -1) {
|
return "";
|
}
|
|
return fileName.substring(idx).toLowerCase();
|
}
|
|
public static String getMime(String ext) {
|
switch (ext) {
|
case ".tif":
|
case ".tiff":
|
return "image/tiff";
|
case ".img":
|
return "application/x-img";
|
case ".gif":
|
return "image/gif";
|
case ".jpg":
|
case ".jpeg":
|
return "image/jpeg";
|
case ".png":
|
return "image/png";
|
case ".mp3":
|
return "audio/mp3";
|
case ".mp4":
|
return "video/mpeg4";
|
case ".avi":
|
return "video/avi";
|
case ".mpg":
|
case ".mpeg":
|
return "video/mpg";
|
case ".wav":
|
return "audio/wav";
|
case ".wma":
|
return "audio/x-ms-wma";
|
case ".swf":
|
return "application/x-shockwave-flash";
|
case ".wmv":
|
return "video/x-ms-wmv";
|
case ".rm":
|
return "application/vnd.rn-realmedia";
|
case ".rmvb":
|
return "application/vnd.rn-realmedia-vbr";
|
case ".js":
|
return "application/x-javascript";
|
case ".css":
|
return "text/css";
|
case ".asp":
|
return "text/asp";
|
case ".mht":
|
return "message/rfc822";
|
case ".jsp":
|
case ".htm":
|
case ".html":
|
case ".xhtml":
|
return "text/html";
|
case ".xml":
|
case ".svg":
|
return "text/xml";
|
case ".txt":
|
return "text/plain";
|
case ".dbf":
|
return "application/x-dbf";
|
case ".mdb":
|
return "application/msaccess";
|
case ".pdf":
|
return "application/pdf";
|
case ".ppt":
|
case ".pptx":
|
return "application/x-ppt";
|
case ".doc":
|
case ".docx":
|
return "application/msword";
|
case ".xls":
|
case ".xlsx":
|
return "application/vnd.ms-excel";
|
case ".dgn":
|
return "application/x-dgn";
|
case ".dwg":
|
return "application/x-dwg";
|
case ".ext":
|
return "application/x-msdownload";
|
default:
|
return "application/octet-stream";
|
}
|
}
|
}
|