restlet 過濾器實現靜態文件編碼

 

restlet如何實現filter參考 : css

http://www.javashuo.com/article/p-unackcrg-bw.htmlhtml

下面是實現靜態文件編碼(解決亂碼問題)的代碼工具

    protected void afterHandle(Request request, Response response) {
        Reference ref=request.getResourceRef();
        String originalPath=ref.getPath();//原始路徑
        String extension = PathUtil.getStaticSourcePathExtension(originalPath);
        if(extension.equalsIgnoreCase("js")
                || extension.equalsIgnoreCase("css")
                || extension.equalsIgnoreCase("html")
                || extension.equalsIgnoreCase("htm")
                ) {
            response.getEntity().setCharacterSet(CharacterSet.UTF_8);
        }

        super.afterHandle(request, response);
    }

 

工具類 PathUtil 的代碼片斷編碼

    /**
     * <p>Title: getPathExtension</p>  
     * <p>Description: 獲取靜態文件路徑的後綴
     *     <br>示例 : <pre>
     *     http://127.0.0.1:8080/aaa/bbb  ==>  ''
     *     http://127.0.0.1:8080/aaa/bbb?t=12  ==>  ''
     *     http://127.0.0.1:8080/aaa/bbb.js  ==>  'js'
     *     http://127.0.0.1:8080/aaa/bbb.js?t=123  ==>  'js'
     *     http://127.0.0.1:8080/aaa/bbb.js?t=123&a=1  ==>  'js'
     *     http://127.0.0.1:8080/aaa/bbb.css?t=123&a=1  ==>  'css'
     *     http://127.0.0.1:8080/aaa/bbb.css.bak?t=123&a=1  ==>  'bak'
     *     </pre>
     * </p>  
     * @param path
     * @return
     */
    public static String getStaticSourcePathExtension(String path) {
        if(StringUtils.isEmpty(path)) {
            return null ;
        }
        String str = null ;
        int url_param_split = path.indexOf('?');
        if(-1 != url_param_split) {
            str = path.substring(0,url_param_split);
        }else {
            str = new String(path);
        }
        int extension_split = str.lastIndexOf('.');
        if(-1 == extension_split) {
            return "";//沒有後綴 , 顯示空字符串
        }
        str = str.substring(extension_split+1);
        return str ;
    }
}
相關文章
相關標籤/搜索