예제 #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();
     }
 }
예제 #2
0
        protected override void doGet(HttpServletRequest request, HttpServletResponse response)
        {
            response.setContentType("text/plain; charset=UTF-8");
            StreamWriter output = response.getWriter();

            String postalCode = request.getParameter("postalCode");
            String ret;

            if (postalCode.Equals("162-0846"))
            {
                ret = "東京都新宿区市谷左内町";
            }
            else if (postalCode.Equals("100-0014"))
            {
                ret = "東京都千代田区永田町";
            }
            else
            {
                ret = "不明";
            }
            response.setStatus(HttpServletResponse.SC_OK);
            output.WriteLine(ret);
        }
		override protected void service(HttpServletRequest req, HttpServletResponse resp)
		{
			resp.setHeader("X-Powered-By", "ASP.NET");
			resp.setHeader("X-AspNet-Version", "1.1.4322");

			String filename = getServletContext().getRealPath(req.getServletPath());
			ServletOutputStream hos;
			try {
				hos = resp.getOutputStream();
			}
			catch (java.lang.IllegalStateException e)
			{
				string mimeType = getServletContext().getMimeType(filename);
				if (mimeType == null || mimeType.StartsWith("text")) {
					sendFileUsingWriter(resp, filename);
					return;
				}
				else
					throw e;
			}
			try 
			{
				string mimeType = this.getServletContext().getMimeType(filename);
				if (mimeType == null)
					mimeType = "text/plain";
				
				resp.setContentType(mimeType);

				FileStream fis = null;
				try {
					fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
					byte[] buf = new byte[4 * 1024];  // 4K buffer
					int bytesRead;
					while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
						   bytesRead != 0) {
						hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
					}
				}
				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.");
				hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
				hos.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);
				hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
				hos.flush();
			}
		}
		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();
			}
		}
예제 #5
0
        override protected void service(HttpServletRequest req, HttpServletResponse resp)
        {
            resp.setHeader("X-Powered-By", "ASP.NET");
            resp.setHeader("X-AspNet-Version", "1.1.4322");

            String filename = getServletContext().getRealPath(req.getServletPath());
            ServletOutputStream hos;

            try {
                hos = resp.getOutputStream();
            }
            catch (java.lang.IllegalStateException e)
            {
                string mimeType = getServletContext().getMimeType(filename);
                if (mimeType == null || mimeType.StartsWith("text"))
                {
                    sendFileUsingWriter(resp, filename);
                    return;
                }
                else
                {
                    throw e;
                }
            }
            try
            {
                string mimeType = this.getServletContext().getMimeType(filename);
                if (mimeType == null)
                {
                    mimeType = "text/plain";
                }

                resp.setContentType(mimeType);

                FileStream fis = null;
                try {
                    fis = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    byte[] buf = new byte[4 * 1024];                      // 4K buffer
                    int    bytesRead;
                    while ((bytesRead = fis.Read(buf, 0, buf.Length)) != -1 &&
                           bytesRead != 0)
                    {
                        hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
                    }
                }
                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.");
                hos.print(((HttpException)myExp).GetHtmlErrorMessage());
                hos.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);
                hos.print(((HttpException)myExp).GetHtmlErrorMessage());
                hos.flush();
            }
        }
예제 #6
0
 public override void SendStatus(int statusCode, string statusDescription)
 {
     // setStatus(int, string) is deprecated
     _HttpServletResponse.setStatus(statusCode /*, statusDescription*/);
 }