All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opslab.util.web.WebUtil Maven / Gradle / Ivy

package com.opslab.util.web;

import com.opslab.util.ConvertUtil;
import com.opslab.util.StringUtil;

import java.io.UnsupportedEncodingException;

/**
 * 提供Web相关的个工具类
 */
public class WebUtil {



    /**
     * 对字符串进行编码
     *
     * @param str 需要处理的字符串
     * @param encoding 编码方式
     * @return 编码后的字符串
     */
    public static String escape(String str, String encoding) throws UnsupportedEncodingException {
        if (StringUtil.isEmpty(str)) {
            return "";
        }
        char[] chars =ConvertUtil.bytesToChars(ConvertUtil.encodeBytes(str.getBytes(encoding), '%'));
        return new String(chars);
    }

    /**
     * 对字符串进行解码
     *
     * @param str 需要处理的字符串
     * @param encoding 解码方式
     * @return 解码后的字符串
     */
    public static String unescape(String str,String encoding){
        if(StringUtil.isEmpty(str)){
            return "";
        }
        return UrlUtil.decodeQuery(str, encoding);
    }



    /**
     * HTML标签转义方法
     *
     *  	空格	  
            <	小于号	<
            >	大于号	>
            &	和号	 &
            "	引号	"
            '	撇号 	'
            ¢	分	 ¢
            £	镑	 £
            ¥	日圆	¥
            €	欧元	€
            §	小节	§
            ©	版权	©
            ®	注册商标	®
            ™	商标	™
            ×	乘号	×
            ÷	除号	÷
     */
    public static String unhtml(String content) {
        if (StringUtil.isEmpty(content)) {
            return "";
        }
        String html = content;
        html = html.replaceAll("'", "'");
        html = html.replaceAll("\"", """);
        html = html.replaceAll("\t", "  ");// 替换跳格
        html = html.replaceAll("<", "<");
        html = html.replaceAll(">", ">");
        return html;
    }
    public static String html(String content) {
        if (StringUtil.isEmpty(content)) {
            return "";
        }
        String html = content;
        html = html.replaceAll("'", "'");
        html = html.replaceAll(""", "\"");
        html = html.replaceAll(" ", " ");// 替换跳格
        html = html.replaceAll("<", "<");
        html = html.replaceAll(">", ">");
        return html;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy