<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 38,  comments - 22,  trackbacks - 0
    1。最直接最簡(jiǎn)單的,方式是把文件地址直接放到html頁(yè)面的一個(gè)鏈接中。這樣做的缺點(diǎn)是把文件在服務(wù)器上的路徑暴露了,并且還無(wú)法對(duì)文件下載進(jìn)行其它的控制(如權(quán)限)。這個(gè)就不寫(xiě)示例了。
    2。在服務(wù)器端把文件轉(zhuǎn)換成輸出流,寫(xiě)入到response,以response把文件帶到瀏覽器,由瀏覽器來(lái)提示用戶(hù)是否愿意保存文件到本地。(示例如下)
    <%
    ?response.setContentType(fileminitype);
    ?response.setHeader(
    "Location",filename);
    ?response.setHeader(
    "Cache-Control",?"max-age="?+?cacheTime);
    ?response.setHeader(
    "Content-Disposition",?"attachment;?filename="?+?filename);?//filename應(yīng)該是編碼后的(utf-8)
    ?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的話(huà),還有一種方式就是用Applet來(lái)實(shí)現(xiàn)文件的下載。不過(guò)客戶(hù)首先得信任你的這個(gè)Applet小程序,由這個(gè)程序來(lái)接受由servlet發(fā)送來(lái)的數(shù)據(jù)流,并寫(xiě)入到本地。
    servlet端示例
    ????public?void?service(HttpServletRequest?req,?HttpServletResponse?res)
    ????????????
    throws?ServletException,?IOException?{
    ????????res.setContentType(
    "?text/plain?");
    ????????OutputStream?outputStream?
    =?null;
    ????????
    try?{
    ????????????outputStream?
    =?res.getOutputStream();
    ????????????popFile(srcFile,?outputStream))?;
    //把文件路徑為srcFile的文件寫(xiě)入到outputStream中。
    ????????}?catch?(IOException?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}?

    JApplet端示例
    ???URLConnection?con;
    ????????
    try?{
    ????????????con?
    =?url.openConnection();//url是被調(diào)用的SERVLET的網(wǎng)址?如http://localhost:8080/sendDateSevlet.do??
    ????????????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,?
    "正在從服務(wù)器下載文件內(nèi)容",?in);
    ????????????ProgressMonitor?pMonitor?
    =?pmInputStream
    ????????????????????.getProgressMonitor();
    ????????????pMonitor.setMillisToDecideToPopup(
    3);
    ????????????pMonitor.setMillisToPopup(
    3);
    ????????????String?localfilepath?
    =?localstr?+?filename?;//localfilepath本地路徑,localstr文件文件夾,filename本地文件名
    ???  if(saveFilsaveFilee(localfilepath,pmInputStream)){ //方法saveFilsaveFilee是把輸入流pmInputStream寫(xiě)到文件localfilepath中。????????????????????
         openLocalFile(localfilepath);
    ????????????}



    4。順便把JApplet上傳文件的代碼也貼上來(lái).
    JApplet端示例

    URLConnection?con;
    ????????
    try?{
    ????????????con?
    =?url.openConnection();//url是被調(diào)用的SERVLET的網(wǎng)址?如http://localhost:8080/sendDateSevlet.do?????????
      ???con.setUseCaches(false);
    ????????????con.setDoInput(
    true);
    ????????????con.setDoOutput(
    true);
    ????????????con.setRequestProperty(
    "Content-Type",
    ????????????????
    "application/octet-stream");
    ????????????
    ????????????OutputStream?out?
    =?con.getOutputStream();
    ????????????String?localfilepath?
    =?localstr?+?filename;?//localfilepath本地路徑,localstr文件文件夾,filename本地文件名
    ????????????getOutputStream(localfilepath,out);//文件getOutputStream是把文件localfilepath寫(xiě)到輸出流out中。
    ????????????InputStream?in?=?con.getInputStream();
    ????????????
    return?true;
    ????????}
    catch?(IOException?e)?{
    ???????????????System.out.println(
    "文件上傳出錯(cuò)!");
    ????????????e.printStackTrace();
    ????????}

    servlet端代碼示例
    ????public?void?service(HttpServletRequest?req,?HttpServletResponse?res)
    ????????????
    throws?ServletException,?IOException?{
    ????????res.setContentType(
    "?text/plain?");
    ????????InputStream?inputStream?
    =?null;
    ????????
    try?{
    ????????????inputStream?
    =?res.getInputStream();
    ????????????writefile(srcFile,?inputStream);
    //把輸入流inputStream保存到文件路徑為srcFile的文件中
    ????????}?catch?(IOException?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}?
    //?end?service

    ?總結(jié):在文件的傳輸中是流的形式存在的,在硬盤(pán)上是文件的形式存在的。我們要做的只是通過(guò)HttpServletRequest和HttpServletResponse,或者是response和request來(lái)發(fā)送流和讀取流。以及把文件轉(zhuǎn)換成流或把流轉(zhuǎn)換成文件的操作。
    posted on 2007-02-13 11:20 aaabbb 閱讀(225) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲人成77777在线播放网站不卡| 亚洲男人的天堂一区二区| 亚洲色四在线视频观看| a在线免费观看视频| 亚洲欧洲成人精品香蕉网| 花蝴蝶免费视频在线观看高清版| 久久综合亚洲色HEZYO国产| 国产高潮久久免费观看| 久久精品亚洲福利| 国产精品区免费视频| 亚洲欧洲日韩不卡| 一级女人18毛片免费| 亚洲综合欧美色五月俺也去| 永久中文字幕免费视频网站| 午夜成人无码福利免费视频| 亚洲夜夜欢A∨一区二区三区| 久久99免费视频| 亚洲jjzzjjzz在线观看| 男女啪啪永久免费观看网站| 人禽伦免费交视频播放| 亚洲成AV人片一区二区| 免费精品国产自产拍在 | 亚洲欧洲日本在线| 最近2019中文免费字幕在线观看| 99亚洲精品高清一二区| 成年女人男人免费视频播放| 免费很黄无遮挡的视频毛片| 亚洲国产精品无码专区影院| 五月婷婷综合免费| 日韩精品无码免费视频| 亚洲午夜未满十八勿入| 日本免费一区尤物| 久久免费观看国产精品88av| 亚洲色精品VR一区区三区| 久久久久久A亚洲欧洲AV冫| 中文字幕免费在线看线人 | 一区二区三区免费视频网站| 久久久久亚洲精品日久生情 | 中文字幕不卡免费视频| 亚洲综合色区中文字幕| 中文亚洲成a人片在线观看|