如何确定HTTP Servlet请求的内容类型? [英] How to determine Content Type of a HTTP Servlet Request?

查看:238
本文介绍了如何确定HTTP Servlet请求的内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不读取请求正文的情况下从HttpServletRequest获取内容类型?

How can I get the content type from the HttpServletRequest without reading the request body?

当我使用以下命令时,我得到null:

When I use the following, I get null:

request.getContentType()

当我尝试使用以下内容读取请求正文中的JSON数据时:

When I try to read the JSON data that comes in the request body using the following:

StringBuilder jsonsb = new StringBuilder();
BufferedReader jsonbr = request.getReader();

request.getReader()引发

Caused by: java.lang.NullPointerException: null
    at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106)

我什至尝试使用以下内容,并且能够获得内容类型,但是在声明之后从请求中获取读者时,仍然获得相同的NullPointerException.

I even tried using the following and was able to get the content type but, still getting the same NullPointerException while getting the reader from request after this statement.

request.getHeader("Accept")

推荐答案

您正在处理GET请求吗?该字段可能不包含Content-Types标头,因此您会在其中看到null.

Are you handling a GET request. That might not contain a Content-Types header and therefore you are seeing a null there.

这篇关于如何确定HTTP Servlet请求的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆