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

📄 opennonhtml.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
package ninth;

/**
 * @author wangmj
 * @mail  <a mailto="wangmingjie_2002@hotmail.com">
 * 2005-4-12 2005
 * 
 * OpenNonHtml.java
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

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

/**
 * 这个Servlet用于打开非HTML文件
 */
public class OpenNonHtml extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

	/**
	 * 20050218王明杰增加。
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		//doGet(request,response);
		//通过ServletOutStream打开一个输出流。
		ServletOutputStream out = response.getOutputStream();

		//---------------------------------------------------------------
		//设置输出数据的MIME类型
		//---------------------------------------------------------------

		response.setContentType("application/pdf"); // MIME type for pdf doc

		//---------------------------------------------------------------
		//create an input stream from fileURL
		//---------------------------------------------------------------

		//String fileURL = "http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.pdf";
		String fileURL = "http://localhost/servlet-study/attachments/adobeapp.pdf";
		
		//------------------------------------------------------------
		//Content-disposition header - don't open in browser and
		//set the "Save As..." filename.
		//*There is reportedly a bug in IE4.0 which ignores this...
		//------------------------------------------------------------
		response.setHeader("Content-disposition", "attachment;filename="
				+ "Example.pdf");
//		response.setHeader("Content-disposition", "filename="
//				+ "Example.pdf");
		//不使用“attachment;”,文档就会在IE浏览器中打开,使用了就会出现保存画面。
//在这里随意定义文件的名字,例如“Example.pdf”
		//-----------------------------------------------------------------
		//PROXY_HOST and PROXY_PORT should be your proxy host and port
		//that will let you go through the firewall without authentication.
		//Otherwise set the system properties and use
		// URLConnection.getInputStream().
		//-----------------------------------------------------------------
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {
			//			URL url = new URL("http", PROXY_HOST,
			// Integer.parseInt(PROXY_PORT),
			//					fileURL);
			URL url = new URL(fileURL);
//			//		防火墙
//			//		如果需要通过防火墙,最后一件要考虑的事情就是你的 URL 链接。
//			//		首先应当搜集所用代理服务器的相关信息,例如主机名称和端口号等。
//			//		更多关于如何通过防火墙建立链接的信息,可以参看下面的资料部分。
//			//
//			//		如果使用的是 Java 2,你应该从 URL 对象类中创建一个 URLConnection 对象,
//			//		并设置下列系统属性:
//
//	URLConnection conn = url.openConnection();
//
//			    // Use the username and password you use to
//			    // connect to the outside world
//			    // if your proxy server requires authentication.
//	String authentication = "Basic " + new
//			sun.misc.BASE64Encoder().encode("username:password".getBytes());
//
//	System.getProperties().put("proxySet", "true");
//	System.getProperties().put("proxyHost", PROXY_HOST); // your proxy host
//	System.getProperties().put("proxyPort", PROXY_PORT); // your proxy port
//	conn.setRequestProperty("Proxy-Authorization", authentication);			
			URLConnection conn = url.openConnection();

			// Use Buffered Stream for reading/writing.
			//使用 带缓存的数据流来读写文件
			bis = new BufferedInputStream(conn.getInputStream());
			bos = new BufferedOutputStream(out);

			byte[] buff = new byte[2048];
			int bytesRead;

			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
//final 还可以用吗?2005第一次使用
		} catch (final MalformedURLException e) {
			System.out.println("MalformedURLException.");
			throw e;
		} catch (final IOException e) {
			System.out.println("IOException."+e.getMessage());
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
}

⌨️ 快捷键说明

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