Windows 上的 UTF-8 编码问题
Issue with UTF-8 encoding on Windows
我在 Ubuntu Gnome 14.04 中使用 Netbeans 开发了一个小 Java Swing 应用程序来查找魔兽世界中的角色统计信息 API。一切都按照我的预期进行,除了一个按钮调用一个方法,该方法在默认浏览器中打开 link 到指定角色的配置文件。在 Ubuntu 上,我在 URL 中使用的字符串可以正确呈现,但在 Windows 上则不能。如果我 运行 应用程序(在 Windows 中)使用为 JVM 指定 UTF-8 编码的批处理文件,我就没有这个问题。当 运行 直接从 .jar 文件中使用它时,尽管我尝试将所有字符串编码为 Windows UTF-8。如何使 URL 的格式正确?我假设我缺少某些东西。如果您需要查看更多代码,请告诉我。提前谢谢你。
ArmoryScanner_UI.java
private void openArmoryLink() {
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
armory.setArmoryLink();
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
}
private String formatName(String name) {
String result;
try {
result = new String(name.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
result = "";
}
return result;
}
ArmoryScanner_Backend.java
public void setArmoryLink () {
try {
String baseURL = "https://us.battle.net/wow/en/character/";
String fullURL = (baseURL + realm + "/" + name + "/simple");
System.out.println("Full URL: " + fullURL);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URL(fullURL).toURI());
} else {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + fullURL);
} catch (IOException e) {
System.out.println("I/O exception (non-Windows system)");
}
}
} catch (UnsupportedOperationException e) {
System.out.println("Unsupported OS");
} catch (MalformedURLException e) {
System.out.println("Bad URL");
} catch (IOException e) {
System.out.println("I/O exception.");
} catch (URISyntaxException e) {
System.out.println("Bad URI syntax");
}
}
这个方法很好用:
private void submit() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
setStatistics(armory);
setProgression(armory);
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
this.setCursor(Cursor.getDefaultCursor());
}
Java 在内部使用 Unicode,因此当您 read/write 外部资源时,通常不需要设置编码。
那里你需要明确地拥有它。
在您的情况下,您需要使用 class URL 编码器对 URL 的元素进行编码。
我在 Ubuntu Gnome 14.04 中使用 Netbeans 开发了一个小 Java Swing 应用程序来查找魔兽世界中的角色统计信息 API。一切都按照我的预期进行,除了一个按钮调用一个方法,该方法在默认浏览器中打开 link 到指定角色的配置文件。在 Ubuntu 上,我在 URL 中使用的字符串可以正确呈现,但在 Windows 上则不能。如果我 运行 应用程序(在 Windows 中)使用为 JVM 指定 UTF-8 编码的批处理文件,我就没有这个问题。当 运行 直接从 .jar 文件中使用它时,尽管我尝试将所有字符串编码为 Windows UTF-8。如何使 URL 的格式正确?我假设我缺少某些东西。如果您需要查看更多代码,请告诉我。提前谢谢你。
ArmoryScanner_UI.java
private void openArmoryLink() {
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
armory.setArmoryLink();
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
}
private String formatName(String name) {
String result;
try {
result = new String(name.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
result = "";
}
return result;
}
ArmoryScanner_Backend.java
public void setArmoryLink () {
try {
String baseURL = "https://us.battle.net/wow/en/character/";
String fullURL = (baseURL + realm + "/" + name + "/simple");
System.out.println("Full URL: " + fullURL);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URL(fullURL).toURI());
} else {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + fullURL);
} catch (IOException e) {
System.out.println("I/O exception (non-Windows system)");
}
}
} catch (UnsupportedOperationException e) {
System.out.println("Unsupported OS");
} catch (MalformedURLException e) {
System.out.println("Bad URL");
} catch (IOException e) {
System.out.println("I/O exception.");
} catch (URISyntaxException e) {
System.out.println("Bad URI syntax");
}
}
这个方法很好用:
private void submit() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ArmoryScanner_Backend armory = new ArmoryScanner_Backend();
String name;
String realm;
String locale;
try {
name = new String(jTextField_Name.getText().getBytes("UTF-8"));
realm = jComboBox_Realm.getSelectedItem().toString();
locale = jComboBox_Locale.getSelectedItem().toString();
if (!name.trim().isEmpty()) {
name = formatName(name);
realm = formatRealm(realm);
locale = formatLocale(locale);
armory.setPlayerInfo(name, realm, locale);
if (armory.isCharacterFound()) {
setStatistics(armory);
setProgression(armory);
} else {
showErrorMessage("Character not found.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} else {
showErrorMessage("Please enter a character name.");
jTextField_Name.setText("");
jTextField_Name.setCaretPosition(0);
jTextField_Name.requestFocus();
}
} catch (UnsupportedEncodingException e) {
showErrorMessage("Error converting name to UTF-8\n"
+ e.getMessage());
}
this.setCursor(Cursor.getDefaultCursor());
}
Java 在内部使用 Unicode,因此当您 read/write 外部资源时,通常不需要设置编码。
那里你需要明确地拥有它。
在您的情况下,您需要使用 class URL 编码器对 URL 的元素进行编码。