首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

難題_高分呕血求助servlet高手幫忙

2011-11-18 
難題_高分吐血求助servlet高手幫忙各位高手大大:我有一個在線打開excel,word...的OpenFileservlet,代碼如

難題_高分吐血求助servlet高手幫忙
各位高手大大:
      我有一個在線打開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前後折磨了我一星期,實在是沒辦法了,求助高手幫忙修改代碼,畫龍點睛....一勞永逸



[解决办法]
o
[解决办法]
private static final String CONTENT_TYPE = "text/html; charset=UTF-8 ";
private String errMsg = "uploadMsg ";

这可不是好习惯
[解决办法]
...........
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();
.......
[解决办法]
先占个位置,再慢慢看
[解决办法]
看了一下你的代码,可能是出现抛出异常后没有关闭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();
}

}

热点排行