java 空格 separater_Java随手记
1.Java路徑在不同系統下的處理,路徑有的時候會進行手動拼接。那么拼接之后可能會出現一些怪異的情況。
例如:C://Test\/Finder
window下是支持/和\路徑分割的。但是有的系統是使用/那么就會出現一些問題。
File.separator,是一個可以獲取當前系統下文件路徑分割符的靜態常量。
那么我做一個小demo,用正則的方式,吧亂七八糟重復的路徑都轉換成當前的系統支持的路徑。
但是還是要記住一件事情,那就是OSX下和Linux下是沒有win下那樣E:\的啦~~~~
String path = "//123\\\\1231231///\\\\1231";
path = path.replaceAll("[\\\\|/]+", File.separator);
System.out.println(path);
/**
* Console:
* /123/123/1231/1231
*/
/**
* 修正win下操作失敗轉義出錯的問題。
* win下默認分割符是\,在java中是轉義符,進行了轉義。
*/
String separator = File.separator;
if(File.separatorChar == 92)
separator = "" + File.separatorChar + File.separatorChar;
String path = "//123\\\\1231231///\\\\1231";
path = path.replaceAll("[/\\\\]+", separator);
System.out.println(path);
2.Java獲取連接中的參數
public static String getUrlParam(String url,String name){
try {
url = java.net.URLDecoder.decode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String exPar = "[?&]" + name + "\\=([^&]+)";
Pattern p = Pattern.compile(exPar);
Matcher m = p.matcher(url);
boolean flag = m.find();
if(!flag) return null;
String parame = m.group();
return parame.substring(parame.indexOf("=") + 1);
}
public static String getUrlParam(String url,String name){
try {
url = java.net.URLDecoder.decode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int index = url.indexOf("&" + name + "=");
if(index == -1) index = url.indexOf("?" + name + "=");
if(index == -1) return null;
int last = url.indexOf("&", index + 1);
if(last == -1) last = url.length();
return url.substring(index + name.length() + 2,last);
}
3.IDEA設置:文件File-設置setting-Keymap 搜索 completion,找到ctrl+空格的快捷鍵,刪除舊的值,然后添加新的值alt+/,保存退出,就可以實現代碼提示
4.如果使用Spring4.1.0以上版本,@ResponseBody出現406錯誤,請將jackson版本替換成2.x版本,jackson-annotations-2.4.4.jar、jackson-core-2.4.4.jar、 jackson-databind-2.4.4.jar 。
5.如果一個項目中在Tomcat5中可以正常運行,在Tomcat6中出現jsp文件無法轉成Servlet,可以查看一下EL表達式是否存在">這種雙引號嵌套了雙引號的問題。
6.反射執行當前類里面某對象的方法
/**
* 反射執行某屬性對象的方法
* @param fieldName 屬性對象的名稱
* @param methodName 該對象的方法
* @param args 調用改方法的參數列表
* @return 返回方法的返回值
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private Object invokeMethod(String fieldName,String methodName,Object... args)
throws SecurityException, NoSuchFieldException, IllegalArgumentException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException{
Class> thisClass = this.getClass();
Field field = thisClass.getDeclaredField(fieldName);
Object fieldObject = field.get(this);
Class> fieldClass = fieldObject.getClass();
Class>[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = fieldClass.getMethod(methodName, argsClass);
return method.invoke(fieldObject, args);
}
7.SpringMVC獲取Response和Request
7.1使用注解@ModelAttribute
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
this.request = request;
this.response = response;
this.session = request.getSession();
}
被@ModelAttribute注釋的方法會在此controller每個方法執行前被執行,因此對于一個controller映射多個URL的用法來說,要謹慎使用
7.2加入監聽器
web.xml加入監聽器
org.springframework.web.context.request.RequestContextListener
Controller:
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
其中監聽器方法中的獲取response以驗證不可用,會報錯,轉換異常。
總結
以上是生活随笔為你收集整理的java 空格 separater_Java随手记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在 Unity 中使用 Probui
- 下一篇: Java 对一组时间进行处理 提出连续的