⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initparamservlet.java

📁 j2ee编程技术中的一些代码
💻 JAVA
字号:
/*
 * Created on 2004-6-13
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author haoyulong
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class InitParamServlet extends HttpServlet {
	ServletConfig myconfig;
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		//TODO Method stub generated by Lomboz
		myconfig=config;
	}
	protected void doGet(HttpServletRequest request,
			HttpServletResponse res) throws ServletException, IOException {
		//TODO Method stub generated by Lomboz
	    myconfig=this.getServletConfig();
		res.setContentType( "application/pdf" ); 
		ServletOutputStream out =res.getOutputStream ();
		File pdf =null;
		BufferedInputStream buf=null;
		//获取初始化参数
		String pdfDir = myconfig.getInitParameter("FilePath");
	     if (pdfDir == null || pdfDir.equals(""))
	           throw new ServletException(
	             "Servlet parameters Wrongs");
		try{
		pdf=new File(pdfDir+"sample.pdf");
		res.setContentLength( (int) pdf.length( ) );
		FileInputStream input = new FileInputStream(pdf);
		 buf = new BufferedInputStream(input);
	    int readBytes = 0;
	     //read from the file; write to the ServletOutputStream
	     while((readBytes = buf.read( )) != -1)
	        out.write(readBytes);
		}
		catch(IOException e){
			System.out.println("file not found!");
		}finally {
	             //close the input/output streams
		         if (out != null)
		             out.close( );
		          if (buf != null)
		          buf.close( );
		     }

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -