我想在默认浏览器中打开一个链接,我使用了以下代码。
String myUrl = "http://www.example.com/engine/myProcessor.jsp?Type=A Type&Name=1100110&Char=!";
try {
Desktop.getDesktop().browse(new URI(myUrl));
} catch (IOException err) {
setTxtOutput("Error: "+err.getMessage());
} catch (URISyntaxException err) {
setTxtOutput("Error: "+err.getMessage());
} catch (Exception err) {
setTxtOutput("Error: "+err.getMessage());
}
我收到了URISyntaxException错误信息,显示查询中存在非法字符。
我认为这是由于我的URL中包含?, &, 和!等特殊字符造成的。我尝试使用:
URLEncoder.encode(myUrl, "UTF-8");
但这给了我另一个错误。
Failed to open http%3A%2F%2Fwww.example.com%2F...........
The system cannot find the file specified.
请问您能告诉我如何纠正“URISyntaxException 非法字符”错误吗?