?? opennonhtml.java
字號(hào):
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;
/**
* 這個(gè)Servlet用于打開(kāi)非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);
//通過(guò)ServletOutStream打開(kāi)一個(gè)輸出流。
ServletOutputStream out = response.getOutputStream();
//---------------------------------------------------------------
//設(shè)置輸出數(shù)據(jù)的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;”,文檔就會(huì)在IE瀏覽器中打開(kāi),使用了就會(huì)出現(xiàn)保存畫(huà)面。
//在這里隨意定義文件的名字,例如“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);
// // 防火墻
// // 如果需要通過(guò)防火墻,最后一件要考慮的事情就是你的 URL 鏈接。
// // 首先應(yīng)當(dāng)搜集所用代理服務(wù)器的相關(guān)信息,例如主機(jī)名稱和端口號(hào)等。
// // 更多關(guān)于如何通過(guò)防火墻建立鏈接的信息,可以參看下面的資料部分。
// //
// // 如果使用的是 Java 2,你應(yīng)該從 URL 對(duì)象類中創(chuàng)建一個(gè) URLConnection 對(duì)象,
// // 并設(shè)置下列系統(tǒng)屬性:
//
// 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.
//使用 帶緩存的數(shù)據(jù)流來(lái)讀寫(xiě)文件
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();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -