java 判断是linux系统_java判断是window系统还是Linux系统,并获取其IP地址及文件上传 | 学步园...
這是upload類的方法:
public class Upload {
public static String upload(FormFile formfile,String dirPath,int port){
String savePath="";
String ip="";
try{
String filename = formfile.getFileName().trim(); // 文件名
if (!"".equals(filename)) {
InputStream ins = formfile.getInputStream();
String currentPath=dirPath+ File.separatorChar + filename;? //文件保存路徑
OutputStream os = new FileOutputStream(currentPath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
boolean isWindows=isWindowsOS();? //判斷是否是windows系統
if(isWindows){
InetAddress address=InetAddress.getLocalHost();
ip=address.getHostAddress();? //獲取ip地址
System.out.println("windows ip地址:"+ip);
}
else{?? ?//如果是Linux系統
ip=Tools.getLinuxIP();
System.out.println("linux ip地址:"+ip);
}
savePath="http://"+ip+":"+port+"/channelProject/upload/";?? //保存到數據庫的地址,圖片上傳到服務器端
savePath+=filename;
os.close();
ins.close();
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println("上傳出現錯誤!!");
}
return savePath;
}
/*
* @return true---是Windows操作系統
*/
public static boolean isWindowsOS(){
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if(osName.toLowerCase().indexOf("windows")>-1){
isWindowsOS = true;
}
return isWindowsOS;
}
}
以下是Tools類下的getLinux的方法:
public class Tools {
public static String getLinuxIP() throws SocketException{
//根據網卡取本機配置的IP
Enumeration netInterfaces=NetworkInterface.getNetworkInterfaces();
InetAddress ipAddress = null;
String ip="";
while(netInterfaces.hasMoreElements())
{
NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement();
if(!ni.getName().equals("eth0")){
continue;
}
else{
Enumeration> e2=ni.getInetAddresses();
while(e2.hasMoreElements()){
ipAddress=(InetAddress) e2.nextElement();
if(ipAddress instanceof Inet6Address)
continue;
ip=ipAddress.getHostAddress();
System.out.println("getLinuxIp:"+ip);
}
break;
}
}
return ip;
}
}
總結
以上是生活随笔為你收集整理的java 判断是linux系统_java判断是window系统还是Linux系统,并获取其IP地址及文件上传 | 学步园...的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: arm linux漏洞,GitHub -
- 下一篇: 更新node_kubernetes证书更
