java - 在 doFilter 中获取 httpservletrequest,无需从 ServletRequest 进行类型转换

标签 java servlets httprequest servlet-filters multipart

我在应用程序中引入了过滤器以进行验证。当我调用 doFilter 方法时,

public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain)

我无法使用getParts()doFilter()getParts()可用于HttpServletRequest .

即使我输入来自 ServletRequest 的cast至HttpServletRequest ,我得到空值。

问题是,有什么办法可以访问 HttpServletRequest里面doFilter()没有来自 ServletRequest 的类型转换?或者如何直接访问HttpServletRequest里面doFilter()而不将其传递到方法中?

下面的代码片段我收到错误

for(final Part part : ((HttpServletRequest) servletRequest).getParts())

最佳答案

在 Tomcat 中,这是一个已知但已解决的问题。请参阅此错误报告:https://bz.apache.org/bugzilla/show_bug.cgi?id=49711

现在有一个选项 (allowCasualMultipartParsing) 可以将 Tomcat 配置为允许 getParts() 调用 servlet 过滤器(默认为关闭)。请参阅documentation :

allowCasualMultipartParsing: Set to true if Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called, even when the target servlet isn't marked with the @MultipartConfig annotation (See Servlet Specification 3.0, Section 3.2 for details). Note that any setting other than false causes Tomcat to behave in a way that is not technically spec-compliant. The default is false

关于java - 在 doFilter 中获取 httpservletrequest,无需从 ServletRequest 进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503961/

相关文章:

java.util.Logger 打包在不同操作系统中时的行为有所不同

java - 在 Android SharedPreferences 中使用当前日期作为键

JAVA扫描器在if/else语句中输入字符串

java - BusinessObject 的 IEnterpriseSession 可以像数据库连接一样被缓存吗?

java - 如何从 servlet 动态设置/获取复选框的值

python - 通过 Python 检查网站是否已启动

java - 使用正则表达式从字符串中提取手机号码

java - 无法调用 getWriter(),getOutputStream() 已调用

ios - IOS SDK中下载多张图片时如何显示加载进度

http - 为什么我的上传进度条在传输完成之前就结束了?