2013-12-11 17 views
13

Cerco di aprire il file HTML dal locale (nel mio sistema) dal programma Java. Ho provato un po 'del programma a superare l'overflow dello stack, ma non funziona così tanto.Come aprire il file HTML usando Java?

Per E.G .: Ho questo piccolo file HTML.

<html> 
    <head> 
    Test Application 
    </head> 
    <body> 
    This is test application 
    </body> 
</html> 

Codice mio Java:

Runtime rTime = Runtime.getRuntime(); 
String url = "D:/hi.html"; 
String browser = "C:/Program Files/Internet Explorer/iexplore.exe "; 
Process pc = rTime.exec(browser + url); 
pc.waitFor(); 

Qualsiasi soluzione o suggerimenti apprezzato.

+0

* "Come aprire il file HTML in chrome ...?" * Perché 'Chrome' in contrasto con il browser predefinito dell'utente? –

risposta

30

io preferirei usare browser predefinito

File htmlFile = new File(url); 
Desktop.getDesktop().browse(htmlFile.toURI()); 
+0

@RamonBoza: Sembra funzionare bene, grazie. –

+0

Non ho negato la tua domanda e non so perché qualcuno ha fatto, la tua domanda è valida al 100%, quindi ho votato xD – RamonBoza

+1

Se l'utente ha assegnato un'azione personalizzata "apri con" all'estensione del file come "html" allora questo NON aprirà il browser, ma il programma l'ha collegato con l'utente .... Questa non è affatto una soluzione! – thesaint

5

Ecco il codice per un metodo che non riesce con grazia.

Si noti che la stringa può essere la posizione di un file html.

/** 
* If possible this method opens the default browser to the specified web page. 
* If not it notifies the user of webpage's url so that they may access it 
* manually. 
* 
* @param url 
*   - this can be in the form of a web address (http://www.mywebsite.com) 
*   or a path to an html file or SVG image file e.t.c 
*/ 
public static void openInBrowser(String url) 
{ 
    try 
     { 
      URI uri = new URL(url).toURI(); 
      Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; 
      if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) 
       desktop.browse(uri); 
     } 
    catch (Exception e) 
     { 
      /* 
      * I know this is bad practice 
      * but we don't want to do anything clever for a specific error 
      */ 
      e.printStackTrace(); 

      // Copy URL to the clipboard so the user can paste it into their browser 
      StringSelection stringSelection = new StringSelection(url); 
      Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); 
      clpbrd.setContents(stringSelection, null); 
      // Notify the user of the failure 
      WindowTools.informationWindow("This program just tried to open a webpage." + "\n" 
       + "The URL has been copied to your clipboard, simply paste into your browser to access.", 
        "Webpage: " + url); 
     } 
} 
0
URI oURL = new URI(url); 
Desktop.getDesktop().browse(oURL); 

Oltre a questo, assicurarsi che il file è già aperto nel browser desiderato. Controlla l'icona sul file, Se si presenta come un file di testo, potresti aver già aperto con il file di testo. Quindi modificare il programma predefinito sul programma desiderato.

Problemi correlati