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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

java 判断是linux系统_java判断是window系统还是Linux系统,并获取其IP地址及文件上传 | 学步园...

發布時間:2024/7/19 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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地址及文件上传 | 学步园...的全部內容,希望文章能夠幫你解決所遇到的問題。

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