国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP文件下載功能的4種方法

2024-09-05 00:22:17
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JSP文件下載功能的4種方法,需要的朋友可以參考下

對于網站來說,網站本身常常需要提供一些資源或者資料進行下載,說到下載莫過于最原始的方法就是在網頁上提供下載的網址。今天講述的還有另外的幾種實現文件下載的方法,對于哪種方法更好這也是看自己的需求。

1、最直接最簡單的,方式是把文件地址直接放到html頁面的一個鏈接中。這樣做的缺點是把文件在服務器上的路徑暴露了,并且還無法對文件下載進行其它的控制(如權限)。這個就不寫示例了。

2、在服務器端把文件轉換成輸出流,寫入到response,以response把文件帶到瀏覽器,由瀏覽器來提示用戶是否愿意保存文件到本地,示例如下:

 

 
  1. <% 
  2. response.setContentType(fileminitype); 
  3. response.setHeader("Location",filename); 
  4. response.setHeader("Cache-Control""max-age=" + cacheTime); 
  5. //filename應該是編碼后的(utf-8) 
  6. response.setHeader("Content-Disposition""attachment; filename=" + filename);  
  7. response.setContentLength(filelength); 
  8. OutputStream outputStream = response.getOutputStream(); 
  9. InputStream inputStream = new FileInputStream(filepath); 
  10. byte[] buffer = new byte[1024]; 
  11. int i = -1; 
  12. while ((i = inputStream.read(buffer)) != -1) { 
  13. outputStream.write(buffer, 0, i); 
  14. outputStream.flush(); 
  15. outputStream.close(); 
  16. inputStream.close(); 
  17. outputStream = null
  18. %> 

3、既然是JSP的話,還有一種方式就是用Applet來實現文件的下載。不過客戶首先得信任你的這個Applet小程序,由這個程序來接受由servlet發送來的數據流,并寫入到本地。

servlet端示例

 

 
  1. public void service(HttpServletRequest req, HttpServletResponse res) 
  2. throws ServletException, IOException { 
  3. res.setContentType(" text/plain "); 
  4. OutputStream outputStream = null
  5. try { 
  6. outputStream = res.getOutputStream(); 
  7. //把文件路徑為srcFile的文件寫入outputStream中 
  8. popFile(srcFile, outputStream)) ; 
  9. catch (IOException e) { 
  10. e.printStackTrace();  

JApplet端示例

 

 
  1. URLConnection con; 
  2. try { 
  3. //url是被調用的SERVLET的網址 如 *.do  
  4. con = url.openConnection(); 
  5. con.setUseCaches(false); 
  6. con.setDoInput(true); 
  7. con.setDoOutput(true); 
  8. con.setRequestProperty("Content-Type"
  9. "application/octet-stream"); 
  10. InputStream in = con.getInputStream(); 
  11. ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream 
  12. (pane, "正在從服務器下載文件內容"in); 
  13. ProgressMonitor pMonitor = pmInputStream.getProgressMonitor(); 
  14. pMonitor.setMillisToDecideToPopup(3); 
  15. pMonitor.setMillisToPopup(3); 
  16. //localfilepath本地路徑,localstr文件文件夾,filename本地文件名 
  17. String localfilepath = localstr + filename ; 
  18. //方法saveFilsaveFilee是把輸入流pmInputStream寫到文件localfilepath中  
  19. if(saveFilsaveFilee(localfilepath,pmInputStream)){ 
  20. openLocalFile(localfilepath); 

4、順便把JApplet上傳文件的代碼也貼上來。

JApplet端示例

 

 
  1. URLConnection con; 
  2. try { 
  3. con = url.openConnection(); 
  4. //url是被調用的SERVLET的網址 如 *.do  
  5. con.setUseCaches(false); 
  6. con.setDoInput(true); 
  7. con.setDoOutput(true); 
  8. con.setRequestProperty("Content-Type","application/octet-stream");  
  9. OutputStream out = con.getOutputStream(); 
  10. //localfilepath本地路徑,localstr文件文件夾,filename本地文件名 
  11. String localfilepath = localstr + filename; 
  12. //文件getOutputStream是把文件localfilepath寫到輸出流out中 
  13. getOutputStream(localfilepath,out); 
  14. InputStream in = con.getInputStream(); 
  15. return true
  16. }catch (IOException e) { 
  17. System.out.println("文件上傳出錯!"); 
  18. e.printStackTrace(); 

servlet端代碼示例

 

 
  1. public void service(HttpServletRequest req, HttpServletResponse res) 
  2. throws ServletException, IOException { 
  3. res.setContentType(" text/plain "); 
  4. InputStream inputStream = null
  5. try { 
  6. inputStream = res.getInputStream(); 
  7. //把輸入流inputStream保存到文件路徑為srcFile的文件中 
  8. writefile(srcFile, inputStream); 
  9. catch (IOException e) { 
  10. e.printStackTrace(); 
  11. // end service 

總結:在文件的傳輸中是流的形式存在的,在硬盤上是文件的形式存在的。我們要做的只是通過HttpServletRequest和HttpServletResponse,或者是response和request來發送流和讀取流。以及把文件轉換成流或把流轉換成文件的操作。

以上就是JSP文件下載功能的方法,希望有一種方法可以適合你,幫助大家解決JSP文件下載功能的實現問題。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张掖市| 临高县| 平顶山市| 丹阳市| 保康县| 诸暨市| 通榆县| 荆门市| 新沂市| 邢台市| 启东市| 淮滨县| 阜宁县| 新蔡县| 庆安县| 镇江市| 黎城县| 潮安县| 许昌县| 红桥区| 博罗县| 屏东市| 蒲城县| 宿迁市| 修水县| 陆丰市| 江川县| 庆安县| 汝阳县| 中牟县| 黄山市| 汤原县| 郑州市| 高台县| 习水县| 佛山市| 嘉峪关市| 台北县| 资阳市| 宾阳县| 衡山县|