js|下載
1.最直接最簡單的,方式是把檔案地址直接放到html頁面的一個連結中。這樣做的缺點是把檔案在伺服器上的路徑暴露了,並且還無法對檔案下載進行其它的控制(如許可權)。這個就不寫樣本了。
2.在伺服器端把檔案轉換成輸出資料流,寫入到response,以response把檔案帶到瀏覽器,由瀏覽器來提示使用者是否願意儲存檔案到本地。(樣本如下)
<% response.setContentType(fileminitype); response.setHeader("Location",filename); response.setHeader("Cache-Control", "max-age=" + cacheTime); //filename應該是編碼後的(utf-8) response.setHeader("Content-Disposition", "attachment; filename=" + filename); response.setContentLength(filelength); OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream(filepath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); inputStream.close(); outputStream = null;%> |
3.既然是JSP的話,還有一種方式就是用Applet來實現檔案的下載。不過客戶首先得信任你的這個Applet小程式,由這個程式來接受由servlet發送來的資料流,並寫入到本地。
servlet端樣本
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(" text/plain "); OutputStream outputStream = null; try { outputStream = res.getOutputStream(); //把檔案路徑為srcFile的檔案寫入outputStream中 popFile(srcFile, outputStream)) ; } catch (IOException e) { e.printStackTrace(); } } |
JApplet端樣本
URLConnection con; try { //url是被調用的SERVLET的網址 如 *.do con = url.openConnection(); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestProperty("Content-Type", "application/octet-stream"); InputStream in = con.getInputStream(); ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream(pane, "正在從伺服器下載檔案內容", in); ProgressMonitor pMonitor = pmInputStream.getProgressMonitor(); pMonitor.setMillisToDecideToPopup(3); pMonitor.setMillisToPopup(3); //localfilepath本地路徑,localstr檔案檔案夾,filename本地檔案名稱 String localfilepath = localstr + filename ; //方法saveFilsaveFilee是把輸入資料流pmInputStream寫到檔案localfilepath中 if(saveFilsaveFilee(localfilepath,pmInputStream)){ openLocalFile(localfilepath); } |
4.順便把JApplet上傳檔案的代碼也貼上來.
JApplet端樣本
URLConnection con; try { con = url.openConnection(); //url是被調用的SERVLET的網址 如 *.do con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setRequestProperty("Content-Type","application/octet-stream"); OutputStream out = con.getOutputStream(); //localfilepath本地路徑,localstr檔案檔案夾,filename本地檔案名稱 String localfilepath = localstr + filename; //檔案getOutputStream是把檔案localfilepath寫到輸出資料流out中 getOutputStream(localfilepath,out); InputStream in = con.getInputStream(); return true; }catch (IOException e) { System.out.println("檔案上傳出錯!"); e.printStackTrace(); } |
servlet端程式碼範例
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(" text/plain "); InputStream inputStream = null; try { inputStream = res.getInputStream();//把輸入資料流inputStream儲存到檔案路徑為srcFile的檔案中 writefile(srcFile, inputStream); } catch (IOException e) { e.printStackTrace(); } } // end service |
總結:在檔案的傳輸中是流的形式存在的,在硬碟上是檔案的形式存在的。我們要做的只是通過HttpServletRequest和HttpServletResponse,或者是response和request來發送流和讀取流。以及把檔案轉換成流或把流轉換成檔案的操作。