![](/img/trans.png)
[英]How to write a request body using a stream with the java 11 http client?
[英]How can I write HTTP Request Body to a file in a Java Web App?
这是我的代码:
@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十六进制值,但是为什么我看到文件中的hex值而不是'ğ'本身呢?
编辑:这也不起作用:
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中的缓冲读取器读取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.