使用this.getClass().getClassLoader().getResource("").getPath() 獲取中文名的文件路徑,並向mysql中插入二進制數據,但是發生了錯誤,打印路徑,發現路徑是亂碼,就感覺應該是
編碼的問題,百度查了下,找到別人提供的解決方法,經測試,可行。
http://blog.csdn.net/lzzyok/article/details/7886914
在使用類似這樣:
- this.getClass().getClassLoader().getResource("").getPath()
來獲取文件路徑時,里面的路徑空格會被“%20”代替,這時候如果你用這個獲取到的包含“%20”的路徑來new一個File時,會出現找不到路徑的錯誤。
於是有以下官方解決方法:
- URI uri = new URI(url.toString());
- FileInputStream fis = new FileInputStream(uri.getPath())
但有另一種解決方法:
- configPath = java.net.URLDecoder.decode(configPath,"utf-8");
於是乎,問題解決了……