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

首頁 > 編程 > JSP > 正文

用Jsp來實現文件下載功能的幾種方式

2024-09-05 00:19: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();        }

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來發送流和讀取流。以及把文件轉換成流或把流轉換成文件的操作。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稷山县| 泾阳县| 牡丹江市| 疏附县| 廉江市| 永清县| 锦州市| 治多县| 大丰市| 惠东县| 贵港市| 鄂托克前旗| 南投市| 芜湖市| 简阳市| 万州区| 仁布县| 汪清县| 溧水县| 福州市| 金寨县| 利辛县| 彰化县| 万源市| 宝丰县| 突泉县| 惠来县| 微博| 区。| 北辰区| 兴安盟| 交城县| 罗山县| 巴南区| 广饶县| 丹巴县| 兰溪市| 天水市| 高台县| 济阳县| 若羌县|