Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将HTTP请求正文写入Java Web应用程序中的文件?_Java_Unicode_Utf 8_Io - Fatal编程技术网

如何将HTTP请求正文写入Java Web应用程序中的文件?

如何将HTTP请求正文写入Java Web应用程序中的文件?,java,unicode,utf-8,io,Java,Unicode,Utf 8,Io,这是我的代码: @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final ServletInputStream inputStream = req.getInputStream(); int read; File file = new File("koray.tx

这是我的代码:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    final ServletInputStream inputStream = req.getInputStream();
    int read;
    File file = new File("koray.txt");
    final FileOutputStream fileOutputStream = new FileOutputStream(file);
    while ((read = inputStream.read()) != -1) {
        fileOutputStream.write(read);
    }
    fileOutputStream.flush();
    fileOutputStream.close();
}
当我收到如下HTTP请求时:

beer-characteristics=ğ
在koray.txt中,我将看到:

beer-characteristics=%C4%9F
c49f似乎是“ğ”的utf-8十六进制值,但为什么我在文件中看到十六进制值,而不是“ğ”本身

编辑:这也不起作用:

final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
while ((read = in.read()) != -1) {
    outputStreamWriter.write(read);
}

将输入流包装为:

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

然后以utf-8格式从缓冲读取器读取数据。

用以下内容包装输入流:

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

然后以utf-8格式从缓冲读取器读取数据。

谢谢,但结果将是相同的。谢谢,但结果将是相同的。