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 ; } }