風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數據加載中……

          JSP文件下載及getOutputStream() has already been的解決(ZT)

          JSP文件下載及getOutputStream() has already been的解決- -

          ??????????????????????????????????????

          JSP文件下載及出現getOutputStream() has already been called for this response的解決方法

          http://iamin.blogdriver.com/iamin/1072546.html

          一、采用RequestDispatcher的方式進行

          1、web.xml文件中增加
          ? <mime-mapping>
          ??? <extension>doc</extension>
          ??? <mime-type>application/vnd.ms-word</mime-type>
          ? </mime-mapping>


          2、程序如下:

          <%@page language="java" import="java.net.*" pageEncoding="gb2312"%>
          <%
          ??? response.setContentType("application/x-download");//設置為下載application/x-download
          ??? String filenamedownload = "/系統解決方案.doc";//即將下載的文件的相對路徑
          ??? String filenamedisplay = "系統解決方案.doc";//下載文件時顯示的文件保存名稱
          ??? filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
          ??? response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
          ???
          ??? try
          ??? {
          ??????? RequestDispatcher dispatcher = application.getRequestDispatcher(filenamedownload);
          ??????? if(dispatcher != null)
          ??????? {
          ??????????? dispatcher.forward(request,response);
          ??????? }
          ??????? response.flushBuffer();
          ??? }
          ??? catch(Exception e)
          ??? {
          ??????? e.printStackTrace();
          ??? }
          ??? finally
          ??? {
          ???
          ??? }
          %>


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

          1、web.xml文件中增加
          ? <mime-mapping>
          ??? <extension>doc</extension>
          ??? <mime-type>application/vnd.ms-word</mime-type>
          ? </mime-mapping>


          2、程序如下:

          <%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%><%
          ??? //關于文件下載時采用文件流輸出的方式處理:
          ??? //加上response.reset(),并且所有的%>后面不要換行,包括最后一個;
          ??? //因為Application Server在處理編譯jsp時對于%>和<%之間的內容一般是原樣輸出,而且默認是PrintWriter,
          ??? //而你卻要進行流輸出:ServletOutputStream,這樣做相當于試圖在Servlet中使用兩種輸出機制,
          ??? //就會發生:getOutputStream() has already been called for this response的錯誤
          ??? //詳細請見《More Java Pitfill》一書的第二部分 Web層Item 33:試圖在Servlet中使用兩種輸出機制 270
          ??? //而且如果有換行,對于文本文件沒有什么問題,但是對于其它格式,比如AutoCAD、Word、Excel等文件
          ??? //下載下來的文件中就會多出一些換行符0x0d和0x0a,這樣可能導致某些格式的文件無法打開,有些也可以正常打開。

          ??? response.reset();//可以加也可以不加
          ??? response.setContentType("application/x-download");//設置為下載application/x-download
          ??? // /../../退WEB-INF/classes兩級到應用的根目錄下去,注意Tomcat與WebLogic下面這一句得到的路徑不同,WebLogic中路徑最后沒有/
          ??? System.out.println(this.getClass().getClassLoader().getResource("/").getPath());
          ??? String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系統解決方案.doc";
          ??? String filenamedisplay = "系統解決方案.doc";//系統解決方案.txt
          ??? filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
          ??? response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

          ??? OutputStream output = null;
          ??? FileInputStream fis = null;
          ??? try
          ??? {
          ??????? output? = response.getOutputStream();
          ??????? fis = new FileInputStream(filenamedownload);

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

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

          posted on 2007-01-06 13:38 風人園 閱讀(375) 評論(0)  編輯  收藏 所屬分類: Web

          主站蜘蛛池模板: 桂平市| 镇平县| 遂昌县| 青田县| 拉萨市| 紫金县| 科尔| 应城市| 遵化市| 巴东县| 宜川县| 怀仁县| 来安县| 广水市| 黄浦区| 古浪县| 尼勒克县| 平昌县| 安丘市| 沁水县| 徐水县| 白银市| 金昌市| 绵竹市| 马鞍山市| 巍山| 德江县| 铜陵市| 林周县| 当涂县| 巴青县| 肇东市| 芒康县| 大同县| 米易县| 伽师县| 阿拉善左旗| 山阴县| 满洲里市| 勐海县| 射洪县|