<%
response.setContentType("Application/msWord;CharSet=gbk");
response.setHeader("Content-disposition";"attachment;filename=help.doc");
%>
help.doc放在根目录下面
我用上面的方法客户端能够下载一个word文档,但是是一个空文档,请问是怎么回事?

解决方案 »

  1.   

    因为你根本没有把help.doc的内容写入response里面。先用得到一个OutputStream通过response.getOutputStream().然后把文件help.doc写入这个outputStream。这样文件才有内容塞.
      

  2.   

    <%@page import="java.util.*"%>
    <%@page import="java.io.*"%>
    <%@page import="java.net.*"%>
    <%
    String filename = "";
    if (request.getParameter("file") != null) {
    filename = request.getParameter("file");
    }
    response.setContentType("application/msword");
    response.setHeader("Content-disposition","attachment; filename="+filename);BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
    bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
    bos = new BufferedOutputStream(response.getOutputStream());byte[] buff = new byte[2048];
    int bytesRead;while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff,0,bytesRead);
    }} catch(final IOException e) {
    System.out.println ( "出现IOException." + e );
    } finally {
    if (bis != null)
    bis.close();
    if (bos != null)
    bos.close();
    }
     
     
    请问下这样写有什么错吗?为什么报错啊?
      

  3.   

    outputStream已经被调用过了,好像是和jsp页面本身的out输出流冲突了,怎么能解决啊?各位大哥帮帮忙,感谢啦
      

  4.   

    在jsp页面默认的有一个out(OutputStream)。那你不用从reponse里面重新获得了。直接用out.write(buff,0,bytesRead);这样应该是可以的。你上面的代码如果不时在jsp页面里面在后台的一个java文件里面用应该是对的。代码没有错误。