<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。最直接最簡單的,方式是把文件地址直接放到html頁面的一個鏈接中。這樣做的缺點是把文件在服務(wù)器上的路徑暴露了,并且還無法對文件下載進行其它的控制(如權(quán)限)。這個就不寫示例了。
    2。在服務(wù)器端把文件轉(zhuǎn)換成輸出流,寫入到response,以response把文件帶到瀏覽器,由瀏覽器來提示用戶是否愿意保存文件到本地。(示例如下)
    <%
    ?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的話,還有一種方式就是用Applet來實現(xiàn)文件的下載。不過客戶首先得信任你的這個Applet小程序,由這個程序來接受由servlet發(fā)送來的數(shù)據(jù)流,并寫入到本地。
    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的文件寫入到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寫到文件localfilepath中。????????????????????
         openLocalFile(localfilepath);
    ????????????}



    4。順便把JApplet上傳文件的代碼也貼上來.
    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寫到輸出流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();
    ????????????writefile(srcFile,?inputStream);
    //把輸入流inputStream保存到文件路徑為srcFile的文件中
    ????????}?catch?(IOException?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????}?
    //?end?service

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

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产综合精品中文第一区| 暖暖日本免费在线视频| 亚洲精品少妇30p| xxxx日本在线播放免费不卡| 免费在线观看视频a| 国产亚洲综合久久| 亚洲第一永久AV网站久久精品男人的天堂AV | 国内精品乱码卡1卡2卡3免费 | 亚洲色成人网站WWW永久四虎| 69式国产真人免费视频 | 久久久久亚洲精品天堂| 中文字幕免费在线| 激情综合亚洲色婷婷五月APP | 日韩在线看片免费人成视频播放| 亚洲国产系列一区二区三区 | 亚洲AV无码AV男人的天堂| 老汉精品免费AV在线播放| 免费一级毛片在线观看| 一级毛片免费视频网站| 九月丁香婷婷亚洲综合色| 亚洲av永久无码精品秋霞电影秋| 国产伦精品一区二区三区免费迷| 免费视频精品一区二区| 97在线观看永久免费视频| 亚洲黄页网在线观看| 国产精品亚洲mnbav网站| 暖暖在线视频免费视频| 亚洲最大无码中文字幕| 亚洲?V乱码久久精品蜜桃| 亚洲人成网站在线在线观看| 又黄又爽一线毛片免费观看| 国产综合免费精品久久久| 亚洲成电影在线观看青青| 国产乱子伦精品免费女| 国内精品免费在线观看| 亚洲欧洲日产国码无码网站| 在线人成精品免费视频| 国产亚洲视频在线观看网址| 国产亚洲人成网站在线观看不卡 | 又粗又硬免费毛片| 99re在线精品视频免费|