栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用utf8字符正确读取url内容?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用utf8字符正确读取url内容?

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) ;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号