zoukankan      html  css  js  c++  java
  • restlet 过滤器实现静态文件编码

    restlet如何实现filter参考 : 

    https://www.cnblogs.com/hi-gdl/p/11451648.html

    下面是实现静态文件编码(解决乱码问题)的代码

        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 ;
        }
    }
  • 相关阅读:
    Linux合并iso
    Oracle中使用escape关键字实现like匹配特殊字符,以及&字符的转义
    hash算法-time33算法
    理解JMS规范中消息的传输模式和消息持久化
    Oracle启动
    Weblogic缓存
    shell
    Hibernate 延迟载入
    Android获取cpu使用率,剩余内存和硬盘容量
    TestNG的组測试和组中组測试
  • 原文地址:https://www.cnblogs.com/hi-gdl/p/11452713.html
Copyright © 2011-2022 走看看