java判斷是window系統還是Linux系統,並擷取其IP地址及檔案上傳__Linux

來源:互聯網
上載者:User

這是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;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.