0

I am trying to save the response as HTML to a file, but there is no method available on HttpServletResponse response to retrieve HTML.

 org.springframework.web.servlet.View review = this.vr.resolveViewName("report2", Locale.US);

        review.render(model.asMap(), request, response);

        System.out.println("html"+response.getOutputStream());

I am using spring-web-mvc. I extracted the above code from a controller.

How can I write the HTML content to file?

UPDATE

public class PDFServletResponse extends HttpServletResponseWrapper {

    private StringWriter  output;

    public PDFServletResponse(HttpServletResponse response) {
        super(response);
        //output= new CharArrayWriter();
        // TODO Auto-generated constructor stub
    }

     public ServletOutputStream getOutputStream() throws IOException {
            throw new UnsupportedOperationException();
          }

          public String toString() {
            return output.toString();
          }
}

 org.springframework.web.servlet.View review = this.vr.resolveViewName("report2", Locale.US);
       // System.out.println("rendered html : " + response.getContentAsString());
   //   reView.render();
        review.render(model.asMap(), request, response);

        PDFServletResponse responseWrapper = new PDFServletResponse((HttpServletResponse) response);
        //  chain.doFilter(request, responseWrapper);

Here, how do I handle filter?

newday
  • 3,842
  • 10
  • 54
  • 79
  • 1
    How about using an HttpServletResponseWrapper and overriding the getOutputStream() / getWriter() methods to return a FileOutputStream or FileWriter that writes to the file? – dnault Apr 26 '16 at 20:49
  • thanks for the hint. I will give a try and will update the post. – newday Apr 26 '16 at 20:53
  • 1
    You can also just change where System.out is pointing – Nadir Apr 26 '16 at 20:54
  • In HttpServletResponseWrapper class I don't see getOutputStream()/getWriter() methods to overwrite. Have I missed anything here? – newday Apr 26 '16 at 21:28
  • 1
    getOutputStream() and getWriter() are inherited from the superclass javax.servlet.ServletResponseWrapper – dnault Apr 26 '16 at 21:43
  • Create a FileOutputStream and write to it? – hd1 Apr 27 '16 at 00:14

0 Answers0