<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一顆開花的樹

    談笑但看風(fēng)云起,龍騰尚待驚蟄春.

     

    Java中頁面重定向亂碼終極解決方案-----java, weblogic, websphere, aix,unix,linux,windows,中英文等任何語言

      1import java.io.ByteArrayOutputStream;
      2import javax.servlet.http.HttpServletRequest;
      3
      4public class URLDecoder {
      5    
      6    /**
      7     * 前言:
      8     * 多年java開發(fā),在各服務(wù)器,中間件以及各異的字符集環(huán)境中,深受頁面重定向編碼亂碼之苦,終于痛下決心解決這個問題。
      9     * 思路:
     10     * 在重定向前 將   &a=b  的名值對編碼為  16進(jìn)制
     11     * 在request.getparameter("")時將   重定向前的已經(jīng)編碼的字符串進(jìn)行解碼 
     12     * 
     13     * Java中頁面重定向亂碼終極解決方案-----java, weblogic, websphere, aix,unix,linux,windows,中英文等任何語言
     14     */

     15    
     16    /**
     17     * 16進(jìn)制數(shù)字字符集
     18     */

     19    private static String hexString = "0123456789ABCDEF ";
     20    public final static String NothingStr = "";
     21    public final static String NullStr = "null#NULL#Null";
     22    
     23    
     24    
     25    
     26    /**
     27     * 用于頁面的名值對取值方法
     28     */

     29    public static String getParameter(HttpServletRequest request, String parameter) {
     30        if (isNull(parameter)) {
     31            return "";
     32        }

     33        String value = "";
     34        // 先編碼查詢串
     35        String params = enCodeHex(parameter);
     36        // 取出對應(yīng)的值
     37        value = (String) request.getParameter(params);
     38        // 解碼值
     39        value = deCodeHex(value);
     40        return value;
     41    }

     42    
     43    
     44    /**
     45     * 編碼QueryString
     46     * @param str
     47     * @return
     48     */

     49    public static String encodeQueryString(String params) {
     50        if (isNull(params)) {
     51            return "";
     52        }

     53        StringBuffer hexStrBuffer = new StringBuffer();
     54        if (params.startsWith("?")) {
     55            // ?開頭的queryString先處理?
     56            hexStrBuffer.append("?");
     57            String firstStr = params.substring(params.indexOf("?"+ 1, params.indexOf("&"));
     58            String strIn[] = firstStr.split("=");
     59            if (strIn.length == 2{
     60                hexStrBuffer.append(enCodeHex(strIn[0]));
     61                hexStrBuffer.append("=");
     62                hexStrBuffer.append(enCodeHex(strIn[1]));
     63            }

     64            params = params.substring(params.indexOf("&"));
     65        }

     66        String strOut[] = params.split("&");
     67        boolean bool = false;
     68        for (int i = 0; i < strOut.length; i++{
     69            String strIn[] = strOut[i].split("=");
     70            if (strIn.length == 2{
     71                if (params.startsWith("&")) {
     72                    bool = true;
     73                }
     else if (!params.startsWith("&"&& i > 0{
     74                    bool = true;
     75                }

     76                if (bool) {
     77                    hexStrBuffer.append("&");
     78                }

     79                hexStrBuffer.append(enCodeHex(strIn[0]));
     80                hexStrBuffer.append("=");
     81                hexStrBuffer.append(enCodeHex(strIn[1]));
     82            }

     83        }

     84        return hexStrBuffer.toString();
     85    }

     86    
     87    /**
     88     * 解碼QueryString
     89     * @param params
     90     * @return
     91     */

     92    public static String deCodeString(String params) {
     93        System.out.println("URLDecoder  deCodeString: " + params);
     94        if (isNull(params)) {
     95            return "";
     96        }

     97        StringBuffer hexStrBuffer = new StringBuffer();
     98        // ?開頭的queryString先處理?
     99        if (params.startsWith("?")) {
    100            hexStrBuffer.append("?");
    101            String firstStr = params.substring(params.indexOf("?"+ 1, params.indexOf("&"));
    102            String strIn[] = firstStr.split("=");
    103            if (strIn.length == 2{
    104                hexStrBuffer.append(deCodeHex(strIn[0]));
    105                hexStrBuffer.append("=");
    106                hexStrBuffer.append(deCodeHex(strIn[1]));
    107            }

    108            params = params.substring(params.indexOf("&"));
    109        }

    110        String strOut[] = params.split("&");
    111        boolean bool = false;
    112        for (int i = 0; i < strOut.length; i++{
    113            String strIn[] = strOut[i].split("=");
    114            if (strIn.length == 2{
    115                if (params.startsWith("&")) {
    116                    bool = true;
    117                }
     else if (!params.startsWith("&"&& i > 0{
    118                    bool = true;
    119                }

    120                if (bool) {
    121                    hexStrBuffer.append("&");
    122                }

    123                hexStrBuffer.append(deCodeHex(strIn[0]));
    124                hexStrBuffer.append("=");
    125                hexStrBuffer.append(deCodeHex(strIn[1]));
    126            }

    127        }

    128        return hexStrBuffer.toString();
    129    }

    130    
    131    
    132    /**
    133     * 判斷是否是空串
    134     * 
    135     * @param s
    136     * @return
    137     */

    138    private static boolean isNull(String s) {
    139        if (s == null || (NullStr.indexOf(s) > -1 || s.trim().equals(NothingStr))
    140                || "undefined".equals(String.valueOf(s))) {
    141            return true;
    142        }

    143        return false;
    144    }

    145    
    146    /*
    147     * 將字符串編碼成16進(jìn)制數(shù)字,適用于所有字符(包括中文)
    148     */

    149    private static String enCodeHex(String str) {
    150        if (isNull(str)) {
    151            return "";
    152        }

    153        // 根據(jù)默認(rèn)編碼獲取字節(jié)數(shù)組
    154        byte[] bytes = str.getBytes();
    155        StringBuilder sb = new StringBuilder(bytes.length * 2);
    156        // 將字節(jié)數(shù)組中每個字節(jié)拆解成2位16進(jìn)制整數(shù)
    157        for (int i = 0; i < bytes.length; i++{
    158            sb.append(hexString.charAt((bytes[i] & 0xf0>> 4));
    159            sb.append(hexString.charAt((bytes[i] & 0x0f>> 0));
    160        }

    161        return sb.toString();
    162    }

    163
    164    /*
    165     * 將16進(jìn)制數(shù)字解碼成字符串,適用于所有字符(包括中文)
    166     */

    167    private static String deCodeHex(String bytes) {
    168        if (isNull(bytes)) {
    169            return "";
    170        }

    171        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
    172        // 將每2位16進(jìn)制整數(shù)組裝成一個字節(jié)
    173        for (int i = 0; i < bytes.length(); i += 2{
    174            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
    175        }

    176        return new String(baos.toByteArray());
    177    }

    178}

    179

    posted on 2010-05-31 21:23 澤來-王者之劍 閱讀(1455) 評論(0)  編輯  收藏 所屬分類: j2se

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 久久嫩草影院免费看夜色| 久久精品国产亚洲AV无码娇色| 亚洲高清一区二区三区电影| 在线观看无码AV网站永久免费| 亚洲色成人网一二三区| 无码国产精品一区二区免费3p| 亚洲av日韩综合一区在线观看| 免费无码毛片一区二区APP| 亚洲国产精品lv| 免费福利视频导航| 久久亚洲精品专区蓝色区| 香蕉高清免费永久在线视频| 亚洲aⅴ天堂av天堂无码麻豆| 免费又黄又硬又爽大片| 好湿好大好紧好爽免费视频| 亚洲AV无码国产精品色午友在线| 91青青青国产在观免费影视| 精品丝袜国产自在线拍亚洲| 四虎影在线永久免费观看| 国产特黄一级一片免费| 亚洲午夜视频在线观看| 成人黄18免费视频| 一级成人a免费视频| 亚洲国产精品第一区二区| 大地资源在线观看免费高清| 免费无遮挡无码视频在线观看| 亚洲精品无码久久久久去q| 麻豆高清免费国产一区| 色偷偷亚洲第一综合网| 亚洲精品无码久久一线| 成年大片免费视频| 好男人资源在线WWW免费| 亚洲人成网站18禁止久久影院| 四虎永久在线精品免费观看地址 | 亚洲欧洲国产成人综合在线观看| 中国一级毛片视频免费看| 亚洲一区电影在线观看| 亚洲午夜无码片在线观看影院猛 | 日本最新免费不卡二区在线| 91视频免费观看| 亚洲欧美成aⅴ人在线观看|