读书人

_高分呕血求助servlet高手忙

发布时间: 2011-11-18 22:58:52 作者: rapoo

_高分吐血求助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(
"           ");
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();
}

}

读书人网 >Java Web开发

热点推荐