欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
瀏覽器從服務(wù)器下載文件的Servlet實(shí)例


1.工具類(lèi)downloadUtils.java的核心部分

  1. //服務(wù)器使客戶(hù)端可以從遠程url下載文件  
  2.     public void download(String fileUrl, HttpServletResponse response) throws ServletException, IOException {  
  3.         String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") +1, fileUrl.length());// 文件名稱(chēng)  
  4.         URL url = new URL(fileUrl);  
  5.         URLConnection conn = url.openConnection();  
  6.         int filesize = conn.getContentLength(); // 取數據長(cháng)度  
  7.         BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());  
  8.         // 清空response  
  9.         response.reset();  
  10.         // 文件名稱(chēng)轉換編碼格式為utf-8,保證不出現亂碼,這個(gè)文件名稱(chēng)用于瀏覽器的下載框中自動(dòng)顯示的文件名  
  11.         response.addHeader("Content-Disposition",  
  12.                 "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));  
  13.         response.addHeader("Content-Length", "" + filesize);  
  14.         BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());  
  15.         response.setContentType("application/octet-stream");  
  16.         // 從輸入流中讀入字節流,然后寫(xiě)到文件中  
  17.         byte[] buffer = new byte[1024];  
  18.         int nRead;  
  19.         while ((nRead = bis.read(buffer, 0, 1024)) > 0) { // bis為網(wǎng)絡(luò )輸入流  
  20.         os.write(buffer, 0, nRead);  
  21.         }  
  22.         bis.close();  
  23.         os.flush();  
  24.         os.close();  
  25.         }  

2.Servlet核心部分webxmlServlet.java(只需看doGet方法即可,其他的可不看):

  1. package test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. /** 
  12.  * Servlet implementation class webxmlServlet 
  13.  */  
  14. public class webxmlServlet extends HttpServlet {  
  15.     private static final long serialVersionUID = 1L;  
  16.          
  17.     /** 
  18.      * @see HttpServlet#HttpServlet() 
  19.      */  
  20.     public webxmlServlet() {  
  21.         super();  
  22.         // TODO Auto-generated constructor stub  
  23.     }  
  24.       
  25.     public void init()  
  26.     {  
  27.         String userName;  
  28.         String userName1;  
  29.         String path;  
  30.         try {  
  31.             ServletConfig config = getServletConfig();  
  32.             userName = config.getInitParameter("username");  
  33.             userName1 = this.getInitParameter("username");  
  34.             path = config.getInitParameter("path");  
  35.             System.out.println("初始化獲得的userName:"+userName);//初始化獲得的userName:admin  
  36.             System.out.println("初始化獲得的userName1:"+userName1);//初始化獲得的userName1:admin  
  37.             System.out.println("初始化獲得的path:"+path);//初始化獲得的path:null  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42.     /** 
  43.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  44.      */  
  45.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  46.         // TODO Auto-generated method stub  
  47.         response.getWriter().append("Served at: ").append(request.getContextPath());  
  48.         System.out.println("webxmlServlet的doGet開(kāi)始。。。");  
  49.         //1.服務(wù)器使客戶(hù)端從網(wǎng)絡(luò )上下載文件  
  50.         String url  = "http://apps.bdimg.com/libs/accounting.js/0.3.2/accounting.min.js";  
  51.         fileDownload fd = new fileDownload();  
  52. //      fd.download(url, response);  
  53.         //2.服務(wù)器從本地拿文件給和客戶(hù)端  
  54.         String localUrl = "file:///D:/test/test1.txt";  
  55.         fd.download(localUrl, response);  
  56.     }  
  57.   
  58.     /** 
  59.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  60.      */  
  61.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  62.         doGet(request, response);  
  63.     }  
  64.   
  65. }  

注意:doGet方法中寫(xiě)了兩種測試方法:

1.一種是瀏覽器從其他網(wǎng)站下資源,本地的服務(wù)器只是提供這種鏈接下載的服務(wù),使客戶(hù)端可以不需要能訪(fǎng)問(wèn)這個(gè)url即可下載此資源,服務(wù)器代為傳輸下載,即服務(wù)器提供鏈接和傳輸中介服務(wù)。使用http協(xié)議。

2.另一種是瀏覽器從服務(wù)器的本地設備中獲取,即服務(wù)器從自己硬盤(pán)中拿出文件給客戶(hù)端。使用本地文本傳輸協(xié)議file協(xié)議。這種更常見(jiàn)。

3.這兩種服務(wù)都需要使用協(xié)議不管是http協(xié)議還是本地文本傳輸協(xié)議file協(xié)議。

4.doGet中這個(gè)兩種方法必須有分開(kāi)測試,即測試一個(gè)注釋一個(gè),不然一個(gè)測試I/O流關(guān)閉后會(huì )導致另一個(gè)報錯,你也可以new兩次fileDownload。我是為了測試效果,才測一個(gè)注掉一個(gè)。


為了讓你的servlet運行起來(lái),我給出web.xml
3.web.xml(需要手動(dòng)在WEB-INF目錄下手動(dòng)新建目錄classes/log4j.properties)

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  
  3.   <welcome-file-list>  
  4.     <welcome-file>login.jsp</welcome-file>  
  5.   </welcome-file-list>  
  6.   <context-param>  
  7.     <param-name>path</param-name>  
  8.     <param-value>/WEB-INF/config.properties</param-value>  
  9.   </context-param>  
  10.   <context-param>  
  11.     <param-name>log4jConfigLocation</param-name>  
  12.     <param-value>/WEB-INF/classes/log4j.properties</param-value>  
  13.   </context-param>  
  14.   <servlet>  
  15.     <description>This is the description of my J2EE component</description>  
  16.     <display-name>This is the display name of my J2EE component</display-name>  
  17.     <servlet-name>CheckAccount</servlet-name>  
  18.     <servlet-class>com.ht.servlet.CheckAccount</servlet-class>  
  19.     <init-param>  
  20.       <param-name>username</param-name>  
  21.       <param-value>admin</param-value>  
  22.     </init-param>  
  23.   </servlet>  
  24.   <servlet-mapping>  
  25.     <servlet-name>CheckAccount</servlet-name>  
  26.     <url-pattern>/CheckAccount</url-pattern>  
  27.   </servlet-mapping>  
  28.   <servlet>  
  29.     <description>This is the description of my J2EE component</description>  
  30.     <display-name>This is the display name of my J2EE component</display-name>  
  31.     <servlet-name>webxmlServlet</servlet-name>  
  32.     <servlet-class>test.webxmlServlet</servlet-class>  
  33.     <init-param>  
  34.       <param-name>username</param-name>  
  35.       <param-value>admin</param-value>  
  36.     </init-param>  
  37.   </servlet>  
  38.   <servlet-mapping>  
  39.     <servlet-name>webxmlServlet</servlet-name>  
  40.     <url-pattern>/webxmlServlet</url-pattern>  
  41.   </servlet-mapping>  
  42.   <session-config>  
  43.     <session-timeout>40</session-timeout>  
  44.   </session-config>  
  45.   <error-page>  
  46.     <error-code>404</error-code>  
  47.     <location>/login.jsp</location>  
  48.   </error-page>  
  49.   <error-page>  
  50.     <error-code>500</error-code>  
  51.     <location>/login.jsp</location>  
  52.   </error-page>  
  53.     
  54.  <!--  <filter>  
  55.   <filter-name>Encoding</filter-name>  
  56.   <filter-class>ghjf.test.filter.SetCharacterEncodingFilter</filter-class>  
  57.   <init-param>  
  58.      <param-name>encoding</param-name>  
  59.      <param-value>UTF-8</param-value>  
  60.   </init-param>  
  61. </filter>  
  62.   
  63. <filter-mapping>  
  64.    <filter-name>Encoding</filter-name>  
  65.    <url-pattern>/*</url-pattern>  
  66. </filter-mapping> -->  
  67. </web-app>  
在啟動(dòng)servlet后,需要測試。

4.用瀏覽器打開(kāi)點(diǎn)擊鏈接測試的HTML頁(yè)面testServletDownload.html:

  1. <!doctype html>  
  2. <html>  
  3.  <head>  
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.   <title>測試客戶(hù)端下載文件頁(yè)面</title>  
  6.  </head>  
  7.  <body>  
  8. <a href="http://localhost:8080/demoProj/webxmlServlet">點(diǎn)我下載文件</a>  
  9.  </body>  
  10. </html>  



本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
servet取得web.xml的初始值
jsp/servlet中 forward, include, reDirect 之間的區別
jsp中的config對象
CAS RESTful API 開(kāi)發(fā)文檔
Restlet 學(xué)習筆記
Java持久性API(JPA)第2講
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久