Java filter failing to set response headers

前端 未结 4 1885
太阳男子
太阳男子 2021-01-11 15:14

I am trying to create a Java \"Filter\" which detects a custom HTTP Request Header, and inserts response headers so that the file will download automatically. The response

4条回答
  •  醉酒成梦
    2021-01-11 15:50

    Assuming you use a response wrapper as described here by others, the whole secret is when to call getWriter() on the original response! That's because the response object ignores all headers added AFTER you asked for a writer!

    So, make sure you add all your headers Before you call getWriter(). Here is my recommended sequence for doFilter():

    1. Create a response wrapper

    2. chain.doFilter (origRequest, wrapper);

    3. Assign all required headers to the original (!) Response

    4. Get writer from original response

    5. Copy the wrapper's content to this writer

提交回复
热议问题