How to launch a file protocol URL with an anchor from Java?

前端 未结 5 1120
深忆病人
深忆病人 2021-01-04 09:08

From a Java program, I need to launch the default browser on a local HTML file, pointed to an anchor inside the file. In Java SE 6, the java.awt.Desktop.browse method will

5条回答
  •  一向
    一向 (楼主)
    2021-01-04 09:30

    I just solved this another way, because no amount of quoting or spaces in any of these examples worked for me.

    1 Detect if the file URI has a anchor or query string

    2 If so, create a temp file File tmpfile = File.createTempFile("apphelp", ".html") with a meta-redirect to the actual file URI I desire:

    
    
    
    

    3 Execute the local rundll command using new temporary URI:

    Runtime.getRuntime().exec(
      "rundll32 url.dll,FileProtocolHandler \"" 
      +tmpfile.toURI().toString()+ "\"");
    

    I hope this works for you!

提交回复
热议问题