| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Web帮助类 |
| | | * |
| | | * @author WWW |
| | | * @date 2024-07-16 |
| | | */ |
| | | @Slf4j |
| | | @SuppressWarnings("ALL") |
| | | public class WebHelper { |
| | | public final static String POINT = "."; |
| | | |
| | |
| | | return osName.startsWith("Windows"); |
| | | } |
| | | |
| | | /** |
| | | * 获取CPU核心数 |
| | | */ |
| | | public static int getCpuCores() { |
| | | return Runtime.getRuntime().availableProcessors(); |
| | | } |
| | | |
| | | /** |
| | | * 格式化日期 |
| | | */ |
| | | public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | |
| | | /** |
| | | * 字符串,是否为空null和空格 |
| | | */ |
| | | public static boolean isEmpty(String str) { |
| | | return null == str || "".equals(str.trim()); |
| | | } |
| | | |
| | | /** |
| | | * 获取GUID |
| | | */ |
| | | public static String getGuid() { |
| | | return UUID.randomUUID().toString(); |
| | | } |
| | | |
| | | /** |
| | | * 获取主机IP |
| | | */ |
| | | public static String getHostIp() { |
| | | try { |
| | | return InetAddress.getLocalHost().getHostAddress(); |
| | |
| | | return "127.0.0.1"; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户IP |
| | | */ |
| | | public static String getIpAddress(HttpServletRequest request) { |
| | | String ip = request.getHeader("X-Forwarded-For"); |
| | | if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) { |
| | |
| | | return ip; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间的Timestamp |
| | | */ |
| | | public static Timestamp getCurrentTimestamp() { |
| | | return new Timestamp(System.currentTimeMillis()); |
| | | } |
| | | |
| | | /** |
| | | * 获取Request |
| | | */ |
| | | public static HttpServletRequest getRequest() { |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | |
| | | return servletRequestAttributes.getRequest(); |
| | | } |
| | | |
| | | /** |
| | | * 获取Response |
| | | */ |
| | | public static HttpServletResponse getResponse() { |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | |
| | | return servletRequestAttributes.getResponse(); |
| | | } |
| | | |
| | | /** |
| | | * 输出JSON至页面 |
| | | */ |
| | | public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, Object obj) { |
| | | res.setStatus(status.value()); |
| | | |
| | | return writeStr2Page(res, JSON.toJSONString(obj)); |
| | | } |
| | | |
| | | /** |
| | | * 输出JSON至页面 |
| | | */ |
| | | public static boolean writeJson2Page(HttpServletResponse res, HttpStatus status, String str) { |
| | | res.setStatus(status.value()); |
| | | |
| | |
| | | return writeStr2Page(res, JSON.toJSONString(map)); |
| | | } |
| | | |
| | | /** |
| | | * 输出str至页面 |
| | | */ |
| | | public static boolean writeStr2Page(HttpServletResponse res, HttpStatus status, String str) { |
| | | res.setStatus(status.value()); |
| | | |
| | | return writeStr2Page(res, str); |
| | | } |
| | | |
| | | /** |
| | | * 输出str至页面 |
| | | */ |
| | | public static boolean writeStr2Page(HttpServletResponse res, String str) { |
| | | try { |
| | | res.setContentType("application/json;charset=UTF-8"); |
| | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 输出字节数组 |
| | | */ |
| | | public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException { |
| | | res.setContentType("application/octet-stream"); |
| | | |
| | |
| | | os.close(); |
| | | } |
| | | |
| | | /** |
| | | * 输出Png文件 |
| | | */ |
| | | public static void writePng(String filePath, HttpServletResponse res) throws IOException { |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param file 文件 |
| | | * @param fileName 文件名 |
| | | * @param res 响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | 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); |
| | | // 设置响应头的编码格式为 UTF-8 |
| | | res.setCharacterEncoding("UTF-8"); |
| | | |
| | | // 通过response对象设置响应数据格式(如:"text/plain; charset=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 { |
| | | // 通过response对象,获取到输出流 |
| | | 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 ""; |
| | |
| | | return fileName.substring(idx).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * 获取多用途互联网邮件扩展类型 |
| | | */ |
| | | public static String getMime(String ext) { |
| | | switch (ext) { |
| | | // 图片 |
| | | case ".tif": |
| | | case ".tiff": |
| | | return "image/tiff"; |
| | |
| | | return "image/jpeg"; |
| | | case ".png": |
| | | return "image/png"; |
| | | // 音/视频 |
| | | case ".mp3": |
| | | return "audio/mp3"; |
| | | case ".mp4": |
| | |
| | | return "application/vnd.rn-realmedia"; |
| | | case ".rmvb": |
| | | return "application/vnd.rn-realmedia-vbr"; |
| | | // 网页 |
| | | case ".js": |
| | | return "application/x-javascript"; |
| | | case ".css": |
| | |
| | | case ".xml": |
| | | case ".svg": |
| | | return "text/xml"; |
| | | // 文件 |
| | | case ".txt": |
| | | return "text/plain"; |
| | | case ".dbf": |
| | |
| | | return "application/x-dwg"; |
| | | case ".ext": |
| | | return "application/x-msdownload"; |
| | | // 默认 |
| | | default: |
| | | return "application/octet-stream"; |
| | | } |