Java程序打開指定地址網頁


1、今天遇到了需要手動輸入http地址打開指定網頁的需求,試着做一個用程序打開指定網頁的功能,搜了一下,還真有一個現成的例子,稍加改造,實現自己的需求;

2、代碼不多,兩個文件;如下:

package com.lgp.solr;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JavaFile {
    
    public static List<String> getUrl(String path) {
        List<String> urls = new ArrayList<String>();
        try {
            FileReader reader = new FileReader(path);
            BufferedReader br = new BufferedReader(reader);
            String str = null;
            while ((str = br.readLine()) != null) {
                if(str!=null  && str.startsWith("http")){
                    urls.add(str);
                }
            }
            br.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return urls;
    }

}

這個類主要作用是讀取指定文件的中的url地址,按行讀取,過濾以http開頭的行內容;

package com.lgp.solr;
/////////////////////////////////////////////////////////
//Bare Bones Browser Launch                            //
//Version 1.5 (December 10, 2005)                    //
//By Dem Pilafian                                                //
//支持: Mac OS X, GNU/Linux, Unix, Windows XP//
//可免費使用                                                        //
/////////////////////////////////////////////////////////

import java.io.File;
/**
 * @author Dem Pilafian
 * @author John Kristian
 */
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class BareBonesBrowserLaunch {

    
    public static void main(String[] args) {
        
        String path = System.getProperty("app.home");
        
        if(path==null || "".equals(path)){
            path = System.getProperty("file.path");
            if(path==null || "".equals(path))
                throw new RuntimeException("未配置app.home和file.path");
        }
        List<String> urls =  new ArrayList<String>();
        
        if(new File(path).isDirectory()){
            File dir = new File(path);
            for (File file : dir.listFiles()) {
                urls.addAll(JavaFile.getUrl(file.getAbsolutePath()));
            }
        }else{
            urls.addAll(JavaFile.getUrl(path));
        }
        
        for (String url : urls) {
            openURL(url);
        }
        
    }
    
    public static void openURL(String url) {
        try {
            browse(url);
        } catch (Exception e) {
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static void browse(String url) throws Exception {
        //獲取操作系統的名字
        String osName = System.getProperty("os.name", "");
        if (osName.startsWith("Mac OS")) {
            //蘋果的打開方式
            Class fileMgr = Class.forName("com.apple.eio.FileManager");
            Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
            openURL.invoke(null, new Object[] { url });
        } else if (osName.startsWith("Windows")) {
           //windows的打開方式。
            /*String browspath = System.getProperty("brows.path");
            try {
                if(browspath != null){
                    Runtime.getRuntime().exec("browspath " + url);
                }
                //Runtime.getRuntime().exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " + url);
            } catch (Exception e) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            }
            */
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            
        } else {
            // Unix or Linux的打開方式
            String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
            String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++)
                //執行代碼,在brower有值后跳出,
                //這里是如果進程創建成功了,==0是表示正常結束。
                if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
                    browser = browsers[count];
            if (browser == null)
                throw new Exception("Could not find web browser");
            else
                //這個值在上面已經成功的得到了一個進程。
                Runtime.getRuntime().exec(new String[] { browser, url });
        }
    }
}

這是主類,適用於mac和Linux,mac系統,很強大;其中主要使用windows系統,打開默認瀏覽器;

3、打成可執行的jar包:注意設置main方法的路徑,如圖

      

從圖1一路Next,設置jar包路徑后,之后再繼續設置圖2,最后Finish;

4、通過bat文件運行jar:

run.bat文件:當讓前提是設置了javahome和classpath等;

set dir=%CD%
java -Dapp.home=%CD%\config -jar %CD%\auto.jar

在jar的所在路徑新建config文件夾,所以配置文件放到此文件夾內,點擊run.bat測試運行結果。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM