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

首頁 > 編程 > JSP > 正文

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

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

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

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();}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洮南市| 贵阳市| 循化| 鹤峰县| 靖边县| 崇仁县| 淳化县| 东安县| 聂拉木县| 崇信县| 桐梓县| 海口市| 榕江县| 吉木萨尔县| 镇巴县| 鲁甸县| 通海县| 昌平区| 屏边| 靖边县| 永州市| 新巴尔虎左旗| 平南县| 响水县| 万荣县| 潮安县| 高尔夫| 五指山市| 隆化县| 福安市| 当阳市| 岑巩县| 南岸区| 广汉市| 永新县| 乌兰浩特市| 休宁县| 米易县| 策勒县| 即墨市| 南溪县|