Java中Desktop類和批量在瀏覽器開啟URI

來源:互聯網
上載者:User

標籤:desktop   browser   瀏覽器   uri   runtime   

有多個網頁網址,我需要查看網頁是否正常,近來的我發現很蛋疼,我要一個一個地複製網址,放進地址欄裡面,這個一旦網址多個,真心蛋疼,一開始不知道怎麼入手,參考了http://www.jb51.net/article/42460.htm這篇文章,然後自己重新去看了API。


第一種方法是之前寫定時器工具用到的東西。

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.csdn.net");

用的是windows指令的操作,Runtime執行的方法相當於你在dos視窗執行。但是像這樣rundll32 url.dll的指令還真不知道。


第二種方法是使用Java提供的Desktop類。

import java.awt.Desktop;import java.io.File;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;/** * @author ckc * 照著api來 可以用預設瀏覽器開啟uri  預設的郵件開啟發送到uri * 預設程式開啟 編輯檔案 */public class TestDesktop {public static void main(String[] args) {//Desktop對象的建立是通過getDesktop擷取執行個體的Desktop dt = Desktop.getDesktop();//判斷Desktop類是否支援當前平台if(dt.isDesktopSupported()){System.out.println("your platform supproted desktop class!");}//Desktop.Action是枚舉類型 有browse edit open mail print這些系統操作//下面方法可以判斷是否支援該操作System.out.println(dt.isSupported(Desktop.Action.MAIL));System.out.println(dt.isSupported(Desktop.Action.EDIT));System.out.println(dt.isSupported(Desktop.Action.OPEN));System.out.println(dt.isSupported(Desktop.Action.BROWSE));System.out.println(dt.isSupported(Desktop.Action.PRINT));//預設用了記事本File text = new File("e:/test.txt");try {dt.edit(text);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//jsp預設用了Sublime Text2  確實用該編輯器開啟了File jsp = new File("e:/test.jsp");try {dt.open(jsp);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//列印的發生了IO異常 連到系統上的裝置沒有發揮作用 應該是電腦沒有印表機的關係try {dt.print(jsp);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//使用了預設的瀏覽器開啟URI uri;try {uri = new URI("www.csdn.net");dt.browse(uri);} catch (URISyntaxException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

寫完這個之後,進行代碼的修改,只針對瀏覽器批量開啟URI。

import java.awt.Desktop;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;public class Main {public static void test(String uristring) {try {URI uri = new URI(uristring);Desktop dp = Desktop.getDesktop();if (dp.isSupported(Desktop.Action.BROWSE)) {dp.browse(uri);}} catch (IOException e) {System.out.println("Browser open URI fail");} catch (URISyntaxException e) {System.out.println("URI is illegal");}}}

因為針對的是眾多多變的URI,所以不能在程式中寫死為字串數組,最好的方法還是用圖形介面,直接拿之前寫過的Swing工具修改即可。


import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;/** * @author ckc 2015-4-10 14:39:13 *         目前的版本是針對最新的http://download.123cw.cn/AppDownload/ *         App/SingleGame/mm/bydr401.apk 輸入 一組url之後 讓系統預設的瀏覽器 Firefox將他們逐一開啟 *         desktop這個類還是蠻好用的 *  *  */public class TestSingleGame {// 兩個監聽器需要用到的變數—— 兩個文本域 一個輸入一個輸出 將其放到全域變數中static JTextArea text = new JTextArea(25, 40);static JScrollPane outjsp = new JScrollPane();public static void main(String[] args) {JFrame frame = new JFrame("OpenURLInExplorer");// 行數,列數BorderLayout layout = new BorderLayout();// 表單大小,表單位置frame.setSize(600, 500);frame.setLocation(400, 200);Container container = frame.getContentPane();container.setLayout(layout);JTextField field = new JTextField("url地址:請將地址複製於此並按start運行");field.setEditable(false);// int rows int columns// JTextArea text = new JTextArea(15,80);// text.setLineWrap(true); 沒有捲軸的時候 true為超出文字框部分也計入//// 捲軸的添加JScrollPane jsp = new JScrollPane(text);// int rows int columnsBox box = Box.createVerticalBox();JPanel p = new JPanel();BoxLayout blayout = new BoxLayout(p, BoxLayout.Y_AXIS);p.setLayout(blayout);p.add(field);p.add(jsp);// frame.setContentPane(p);// frame.setLayout(layout);JPanel p2 = new JPanel();BoxLayout blayout2 = new BoxLayout(p2, BoxLayout.Y_AXIS);p2.setLayout(blayout2);p2.add(outjsp);JButton b = new JButton("start to open every uri at your default browser");b.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// /n為分行符號 Null 字元串直接跳過String[] array = text.getText().split("\n");for (int i = 0; i < array.length; i++) {Main.test(array[i]);}}});container.add(p, "North");container.add(b, "Center");// frame.pack();frame.setResizable(false);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

最後的結果:

                                  

將網址全部錄入文字框中,按執行後便會在預設的瀏覽器中開啟。

Java中Desktop類和批量在瀏覽器開啟URI

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.