Java中windows路径转换成linux路径等工具类

项目中发现别人写好的操作系统相关的工具类:

我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html

 import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List; /**
* OS Utility Class This is used to obtain the os related var programmatically
*
* <p>
* <a h ref="OSUtil.java.html"><i>View Source</i></a>
* </p>
*
* */
public class OSUtil { public static String LIUNX = "Linux";
public static String WINDOWS = "Windows"; /**
* 功能: 将windows路径转换成linux路径
*/
public static String convert2linuxPath(String _path){
if(isLinuxSystem()){
int index = _path.indexOf(":");
if(index>1 || index == -1) //不含有盘符 return _path; SysstoredevMgr _sM = new SysstoredevMgr() ;
List<Sysstoredev> _list = _sM.findAll() ;
for( Sysstoredev _sd : _list ){
String _driver = ConvertString.ConvertStr(_sd.getDriver()) ;
if(_path.startsWith(_driver)){
return FileNameUtil.correctFileName4Linux(_path.replace(_driver, ConvertString.ConvertStr(_sd.getLpath()))) ;
}
}
}
return _path;
} /**
* 获得主机名称
* obtain the host name in form of string
*
* @return String
* @throws UnknownHostException
*/
public static String getHostName() throws UnknownHostException {
InetAddress inetaddr = InetAddress.getLocalHost();
return inetaddr.getHostName();
} /**
* 获得主机IP
* obtain the ip address of the host in form of string
*
* @return String
* @throws UnknownHostException
*/
public static String getHostIP() throws UnknownHostException {
InetAddress inetaddr = InetAddress.getLocalHost();
return inetaddr.getHostAddress();
} /**
* 测试给定的主机是否是本地主机.
* check if the given host is the local host
*
* @param hostname String
* @param hostip String
* @return boolean
* @throws UnknownHostException
*/
public static boolean isNative(String hostname, String hostip) {
try {
hostname = (hostname == null ? "" : hostname);
hostip = (hostip == null ? "" : hostip); InetAddress inetaddr = InetAddress.getLocalHost();
if (hostname.equals(""))
return inetaddr.getHostAddress().equalsIgnoreCase(hostip); if (!inetaddr.getHostName().equalsIgnoreCase(hostname))
return false; if (hostip.length() > 0) {
InetAddress[] inetaddrs = InetAddress.getAllByName(inetaddr.getHostName());
boolean b = false;
for (int i = 0; i < inetaddrs.length; i++) {
if (inetaddrs[i].getHostAddress().equalsIgnoreCase(hostip)) {
b = true;
break;
}
}
return b;
} else {
return true;
}
} catch (UnknownHostException e) {
return false;
}
} /**
* 获得指定的环境变量的值
* @param envvarname
* @param defaultvalue
* @return
*/
public static String getEnvironmentVar(String envvarname,
String defaultvalue) {
String str = System.getenv(envvarname);
str = (str == null ? "" : str);
return (str.length() == 0 ? defaultvalue : str);
} /**
* 判断是否是Linux操作系统
* @return
*/
public static Boolean isLinuxSystem(){
if(OSUtil.LIUNX.equals(System.getProperty("os.name"))){
return true;
}
return false;
} public static void main(String[] args) throws Exception{ System.out.println(OSUtil.convert2linuxPath("M:\\hello\\abc.txt")); System.out.println(OSUtil.convert2linuxPath("M:\\hello/abc.txt")); System.out.println(OSUtil.convert2linuxPath("/linux/p\\u.png")); } }
上一篇:ASP.NET Core 中的SEO优化(3):自定义路由匹配和生成


下一篇:在ASP.NET Core中使用百度在线编辑器UEditor