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

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

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

    posts - 495,comments - 227,trackbacks - 0

              jsp中實(shí)現(xiàn)文件下載的最簡(jiǎn)單的方式是在網(wǎng)頁(yè)上做超級(jí)鏈接,如:<a href="music/abc.mp3">點(diǎn)擊下載</a>。但是這樣服務(wù)器上的目錄資源會(huì)直接暴露給最終用戶,會(huì)給網(wǎng)站帶來(lái)一些不安全的因素。因此可以采用其它方式實(shí)現(xiàn)下載,可以采用:1、RequestDispatcher的方式進(jìn)行;2、采用文件流輸出的方式下載。

    1、采用RequestDispatcher的方式進(jìn)行

                jsp頁(yè)面中添加如下代碼:
               <%
          response.setContentType("application/x-download");//設(shè)置為下載application/x-download
          String filedownload = "/要下載的文件名";//即將下載的文件的相對(duì)路徑
          String filedisplay = "最終要顯示給用戶的保存文件名";//下載文件時(shí)顯示的文件保存名稱
          filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
          response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
        
          try
          {
              RequestDispatcher dis = application.getRequestDispatcher(filedownload);
              if(dis!= null)
              {
                  dis.forward(request,response);
              }
              response.flushBuffer();
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
          finally
          {
        
          }
    %>

    2、采用文件流輸出的方式下載

             <%@page language="java" contentType="application/x-msdownload"    pageEncoding="gb2312"%><%
          //關(guān)于文件下載時(shí)采用文件流輸出的方式處理:
          //加上response.reset(),并且所有的%>后面不要換行,包括最后一個(gè)

          response.reset();//可以加也可以不加
          response.setContentType("application/x-download");
          String filedownload = "想辦法找到要提供下載的文件的物理路徑+文件名";
          String filedisplay = "給用戶提供的下載文件名";
          filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
          response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

          OutputStream outp = null;
          FileInputStream in = null;
          try
          {
              outp = response.getOutputStream();
              in = new FileInputStream(filenamedownload);

              byte[] b = new byte[1024];
              int i = 0;

              while((i = in.read(b)) > 0)
              {
                  outp.write(b, 0, i);
              }
              outp.flush();
          }
          catch(Exception e)
          {
              System.out.println("Error!");
              e.printStackTrace();
          }
          finally
          {
              if(in != null)
              {
                  in.close();
                  in = null;
              }
              if(outp != null)
              {
                  outp.close();
                  outp = null;
              }
          }
    %>

    在wsad里面寫(xiě)JSP文件下載,總是出現(xiàn)這個(gè)異常,getOutputStream() has already been called for this response,輸出流已經(jīng)被調(diào)用了.

          上網(wǎng)查半天終于明白一點(diǎn),JSP早下載文件的時(shí)候用到了OutputStream,而在Application Server在處理編譯jsp時(shí)對(duì)于%>和<%之間的內(nèi)容一般是原樣輸出,而且默認(rèn)是PrintWriter.

    posted on 2008-01-17 17:10 SIMONE 閱讀(34459) 評(píng)論(7)  編輯  收藏 所屬分類: JAVAJSP

    FeedBack:
    # re: jsp中文件下載的實(shí)現(xiàn)[未登錄](méi)
    2011-09-14 08:54 | 123
    就知道抄 全是錯(cuò)的還不知道  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)
    2011-12-15 15:27 | lisatisfy
    filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
    filenamedisplay 應(yīng)該是filedisplay
    application.getRequestDispatcher(filedownload);
    application最好改成request  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)
    2012-04-28 09:37 |
    application.getRequestDispatcher(filedownload);
    application最好改成request




    改成request后出現(xiàn)的成了提交的jsp頁(yè)面,而不是我要的文件啊  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)[未登錄](méi)
    2013-04-17 17:40 | haha
    @123
    +1+1+1+1  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)[未登錄](méi)
    2014-01-27 17:08 | gaga
    大概過(guò)程明白  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)
    2014-11-01 01:09 | afsd
    是的噶的發(fā)送到  回復(fù)  更多評(píng)論
      
    # re: jsp中文件下載的實(shí)現(xiàn)
    2014-11-25 12:01 | 阿大是大非說(shuō)
    能不能不禍害人??  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 亚洲精品无码高潮喷水在线| 四虎免费影院4hu永久免费| 97se亚洲综合在线| 野花高清在线电影观看免费视频| 日韩亚洲综合精品国产| 久久久久亚洲Av无码专| 国产午夜免费福利红片| 亚洲精品视频在线免费| 亚洲国产精久久久久久久| 中文字幕高清免费不卡视频| 亚洲Av高清一区二区三区| 久久香蕉国产线看观看亚洲片| 国产精品国产午夜免费福利看| 无码AV片在线观看免费| jizz在线免费播放| 久久久久亚洲AV成人网人人网站| 免费手机在线看片| 国产免费MV大全视频网站| 亚洲另类春色国产精品| 亚洲AV日韩AV永久无码绿巨人| 亚洲中文字幕在线观看| 国产成人精品久久亚洲| 搡女人免费免费视频观看| 色拍自拍亚洲综合图区| 亚洲黄色免费网址| 亚洲国产夜色在线观看| 亚洲人成人网站18禁| 亚洲国产成人影院播放| 国产国拍亚洲精品福利| 亚洲国产另类久久久精品小说 | 国产亚洲av片在线观看16女人| 久久精品亚洲男人的天堂 | 黄色免费在线观看网址| 亚洲精品无码不卡在线播HE| 亚洲国产精品一区二区久久| 亚洲精品免费在线观看| 亚洲另类自拍丝袜第五页| caoporm超免费公开视频| 中国xxxxx高清免费看视频| 全免费A级毛片免费看网站 | 中文字幕专区在线亚洲|