各位高手大大:
   我有一個在線打開excel,word...的OpenFile  servlet,代碼如下:
---------------------------------------------------------------------
package pursue.com.web;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import pursue.com.util.Globals;public class Openfile
    extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    private String errMsg = "uploadMsg";    //Initialize global variables
    public void init() throws ServletException {
    }    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
        HttpSession session = request.getSession();
        String filename = request.getParameter("file_name");
        filename = Globals.RTC_UP_PATH + "/" + filename;
        response.setHeader("Content-disposition",
                           "attachment;filename=" +
                           filename.replace('\\', '/').
                           substring(filename.lastIndexOf("/")));
        String ext=filename.substring(filename.lastIndexOf(".")+1);
        if(ext.equalsIgnoreCase("xls")){
            System.out.println("xlsfile:"+filename);
            response.setContentType("application/vnd.ms-excel");
        }else{
            System.out.println("html:"+filename);
            response.setContentType("text/html");
        }
        File doc = new File(filename);
        ServletOutputStream out = response.getOutputStream();
        FileInputStream fin = new FileInputStream(doc);
        int b;
        while ( (b = fin.read()) != -1) {
            out.write(b);
        }
        fin.close();
        out.close();
        //System.out.println("File is open !");
        //JLibrary.Foundation.ErrorHandle.logMessage("File is open !");
        
    }    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
        doGet(request, response);
    }    //Clean up resources
    public void destroy() {
    }    public void doFileNotFound(HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println(
            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("</head>");
        out.println("<body bgcolor=\"#FFCC00\">");
        out.println("<div align=\"center\">");
        out.println("<p><img src=\"" + request.getContextPath() +
                    "/images/error.gif\" width=\"333\" height=\"333\"></p>");
        out.println("<p>" + errMsg + "</p>");
        out.println(
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        out.println(
            "<a href=\"#\" onClick=\"javascript:window.close()\">Close Window</a>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    }
}
-------------------------------------------
在日志裡老報異常
:getOutputStream() has already been called for this response
另外,我在另外一個OpenDoc的servlet中打開word文檔,方式如上,只不過在Open()方法裡加了文件不存在,就調用doFileNotFound()方法,也出現上面的異常.在google搜索後,大家都說response不能同時使用二進制流和字符流兩種輸入方式..
所以我就把doFileNotFound()中的PrintWriter out = response.getWriter();改成
ServletOutputStream out = response.getOutputStream();全部使用二進制流,依然不行,這個bug前後折磨了我一星期,實在是沒辦法了,求助高手幫忙修改代碼,畫龍點睛....一勞永逸

解决方案 »

  1.   

    总觉得 是 调用了 两次重复的 out.println  里有 getOutputStream()
      

  2.   

    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    private String errMsg = "uploadMsg";这可不是好习惯
      

  3.   

    ...........
    if(ext.equalsIgnoreCase("xls")){
                System.out.println("xlsfile:"+filename);
                response.setContentType("application/vnd.ms-excel");
            }else{
                System.out.println("html:"+filename);
                response.setContentType("text/html");
            }BufferedInputStream bis = 
            
                          new BufferedInputStream(
                          
                          new FileInputStream(new String( filename.getBytes("GBK"), "ISO8859_1" )));//chinese
                          
            ServletOutputStream bos = response.getOutputStream();
                          
            byte[] buff = new byte[2048];
            
            int bytesRead;
            
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            
                bos.write(buff,0,bytesRead);
                
            }
            
            bos.flush();
            
            bis.close();
            
            bos.close();
            
            //timeout
            response.setStatus(response.SC_OK );
            
            response.flushBuffer();
    .......
      

  4.   

    看了一下你的代码,可能是出现抛出异常后没有关闭ServletOutputStream类
    可以尝试一下
            File doc = null;
            ServletOutputStream out = null;
            FileInputStream fin = null;        try{
            File doc = new File(filename);
            ServletOutputStream out = response.getOutputStream();
            FileInputStream fin = new FileInputStream(doc);
            int b;
            while ( (b = fin.read()) != -1) {
                out.write(b);
            }
            }catch(IOException e)
            {           e.printStackTrace();
             }
            finally{
            if(fin != null){
            fin.close();
            }
            if(out != null){
            out.close();
            }        }
      

  5.   

    swoky(阿勇)...........可以注釋一下的代碼嗎?
     response.setStatus(response.SC_OK );response.flushBuffer();這兩句能清空二進制流嗎?
    -------------------------------------------------------------------------------lys888(李永生)...謝謝你,不過因為系統在公司的LinuxWeb服務器上,白天不好測試,多謝各位熱情幫忙......等問題解決了,我給大家加分...