byte[] bytes = json.getBytes("UTF-8");为您提供UTF-8字节序列,因此URLReader.read也为您提供UTF-8字节序列
但是您尝试在未指定编码器的
new String(URLReader.read("pl", "en","koń"))情况下使用进行解码,即Java将使用您的系统默认编码进行解码(不是UTF-8)尝试:
new String(URLReader.read("pl", "en", "koń"), "UTF-8")更新资料
这是我机器上的完整工作代码:
public class URLReader { public static byte[] read(String from, String to, String string) { try { String text = "http://translate.google.com/translate_a/t?" + "client=o&text=" + URLEnprer.enpre(string, "UTF-8") + "&hl=en&sl=" + from + "&tl=" + to + ""; URL url = new URL(text); URLConnection conn = url.openConnection(); // Look like faking the request coming from Web browser solve 403 error conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String json = in.readLine(); byte[] bytes = json.getBytes("UTF-8"); in.close(); return bytes; //return text.getBytes(); } catch (Exception e) { System.out.println(e); // becarful with returning null. subsequence call will return NullPointException. return null; } }}别忘了将逃逸到 u0144。Java编译器可能无法正确编译Unipre文本,因此最好以纯ASCII形式编写它。
public class AbcServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/plain;charset=UTF-8"); byte[] read = URLReader.read("pl", "en", "kou0144"); resp.getOutputStream().write(read) ; }}


