Ejemplo n.º 1
0
 void sendFileUsingWriter(HttpServletResponse resp, string filename)
 {
     java.io.PrintWriter writer = resp.getWriter();
     try
     {
         resp.setContentType(this.getServletContext().getMimeType(filename));
         StreamReader fis = null;
         char[]       buf = new char[4 * 1024];            // 4K buffer
         try {
             fis = new StreamReader(filename);
             int charsRead;
             while ((charsRead = fis.Read(buf, 0, buf.Length)) != -1 &&
                    charsRead != 0)
             {
                 writer.write(buf, 0, charsRead);
             }
         }
         finally {
             if (fis != null)
             {
                 fis.Close();
             }
         }
     }
     catch (System.IO.FileNotFoundException e)
     {
         resp.setStatus(404, "Object Not Found.");
         HttpException myExp = new HttpException(404, "File '" + filename + "' not found.");
         writer.print(((HttpException)myExp).GetHtmlErrorMessage());
         writer.flush();
     }
     catch (Exception e)
     {
         Trace.WriteLine(String.Format("ERROR in Static File Reading {0},{1}", e.GetType(), e.Message));
         resp.setStatus(500);
         HttpException myExp = new HttpException("Exception in Reading static file", e);
         writer.print(((HttpException)myExp).GetHtmlErrorMessage());
         writer.flush();
     }
 }