简体   繁体   English

我的简单 Java HTTPServer 中的特殊字符编码

[英]Special character encoding in my simple Java HTTPServer

I have a simple Java application, basically a server implemented using com.sun.net.HttpServer API, that reads a file and simply sends back the texts after some processing.我有一个简单的 Java 应用程序,基本上是一个使用com.sun.net.HttpServer API 实现的服务器,它读取一个文件并在一些处理后简单地发回文本。 The server part simply looks like this:服务器部分看起来像这样:

        server = HttpServer.create(new InetSocketAddress(serverPort), 0);
        logger.info("EventRetriever REST server listening to port: " + serverPort);
        server.createContext("/getEvents", new MedatadaHandler());
        server.setExecutor(null);
        server.start();
// ...
@Override
        public void handle(HttpExchange he) throws IOException {
        //...
        String response = requestEvents();
        he.sendResponseHeaders(200, response.length());
        OutputStream os = he.getResponseBody();
        os.write(response.toString().getBytes());
        os.close();
}
//...
public String requestEvents(){
//...
// this printing on the console looks fine though:
        logger.info(jsonString);
        return jsonString;
}

I run my jar file with java -jar myApp.jar on a command line or simply on my IDE.我在命令行或仅在我的 IDE 上使用java -jar myApp.jar运行我的jar文件。 I'm witnessing some weird behaviors, sometimes just hanging, when it requires sending texts containing special characters, such as the music symbol .我目睹了一些奇怪的行为,有时只是挂起,当它需要发送包含特殊字符的文本时,例如音乐符号 When I call the IP:PORT/getEvent via a browser, the behavior is so weird:当我通过浏览器调用IP:PORT/getEvent ,行为非常奇怪:

If I run it on a Windows Powershell or Command Prompt, the symbol appears as ?如果我在 Windows Powershell 或命令提示符上运行它,符号显示为? on the console, and what I get from the browser is also shown as ?在控制台上,我从浏览器中得到的也显示为? . . But when I run the program on a linux server or my Eclipse IDE, it is shown correctly on the console (as ), but on the browser, I get the following error, although the status is 200 OK .但是当我在 linux 服务器或我的 Eclipse IDE 上运行程序时,它在控制台上正确显示(如 ),但在浏览器上,我收到以下错误,尽管状态为200 OK I see on the console the java application keep looping printing the line every few seconds (as if it is trying to send the data, but can't maybe something is blocking it!).我在控制台上看到 java 应用程序每隔几秒钟就会循环打印该行(好像它正在尝试发送数据,但可能无法阻止它!)。 But I don't get any exception or errors on the app (I log all possible errors).但是我在应用程序上没有收到任何异常或错误(我记录了所有可能的错误)。

在此处输入图片说明

I'm very confused for this behavior.我对这种行为感到非常困惑。 What's going on?!这是怎么回事?!

First, why what I get is dependent on the environment I run my Java app?!首先,为什么我得到的结果取决于我运行 Java 应用程序的环境?! If Windows Command Prompt/Powershell shows the character as ?如果 Windows 命令提示符/Powershell 将字符显示为? , I expect it just showing it locally like that. ,我希望它像那样在本地显示它。 Why should I see it also as ?为什么我也要把它看作? on my browser?!在我的浏览器上?! Java app must be independent of the environment. Java 应用程序必须独立于环境。

And second, what is going on with that error on the Linux/Eclipse envrionment when requesting a line that has this character?其次,在请求具有此字符的行时,Linux/Eclipse 环境中的该错误是怎么回事?

The issue as could be predicted, was related to getBytes() and UTF-8 String representations.可以预测的问题与getBytes()UTF-8字符串表示有关。 Did the following and it was all good then:做了以下事情,然后一切都很好:

        he.sendResponseHeaders(200, response.getBytes("UTF-8").length);
        OutputStream os = he.getResponseBody();
        os.write(response.getBytes("UTF-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM