日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 空格 separater_Java随手记

發布時間:2024/3/13 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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随手记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。