2 Replies Latest reply: Aug 13, 2007 9:53 PM by 800351 RSS

    HTTPResponse.

    807605
      I am having trouble returning large amounts of data on an HTTPResponse.

      Below is the doPost method in my servlet
      protected void doPost(
                HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
                               
                InputStream in = request.getInputStream();     
                
                response.setContentType("text/html");
                response.setContentLength(2147483647);     //Maximum integer value
                
                BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream() );
                   
                   byte[] buffer = new byte[2048];
                int bytesRead = in.read(buffer);          
                while (bytesRead >= 0) {
                     out.write(buffer, 0, bytesRead);
                     bytesRead = in.read(buffer);
                }
                out.flush();
                out.close();
           }
      The code works fine the data passed in on the request is small. However, only part of it is returned when the the input data is very large.

      I think the problem lies in the data exceeding maximum buffer size.

      If I simply print the output data to a text file (instead of HTTPResponse) then everything is printed to the file correctly.

      Any help would be much appreciated.
        • 1. Re: HTTPResponse.
          807605
          Dont worry folks, its all been sorted out. The problem was on the client side not the server side.

          I was initially trying to do something like this to print out what was returned from the servlet to the client:
          byte b[] = new byte[inStream.available()];
          inStream.read(b);
          System.out.println(new String(b));
          However, with this approach not everything that was returned from the servlet was being printed out.

          The following code however does work:
          DataOutputStream out = new DataOutputStream(new FileOutputStream("output.txt"));
          byte[] buffer = new byte[2048];
          int bytesRead = inStream.read(buffer);
          int counter = 0;
          while (bytesRead >= 0) {
              out.write(buffer, 0, bytesRead);
              bytesRead = inStream.read(buffer);
          }
          out.flush();
          out.close();
          • 2. Re: HTTPResponse.
            800351
            Good! Real Java programmer never use available().