比如有个test.doc放在本类目录下,然后我另一个jsp页面需要打开后就显示这个test.doc中的内容,方便我只要改了doc就可以在那个jsp页面中显示。contentType="application/msword"只是把当前页面中的内容在ie中用word打开而不是把指定文件去打开。

解决方案 »

  1.   

    <%@ page contentType="application/msword;charset=gb2312"%>
    <%@ page import="java.io.*"%>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <%
     String path=request.getRealPath("");//取得当前目录的路径
     FileReader fr=new FileReader(path + "\\t.doc");
     BufferedReader br=new BufferedReader(fr);
     String Line=br.readLine();//从文件读取一行字符串
     //判断读取到的字符串是否不为空
     while(Line!=null){
      out.println(Line + "<br>");//输出从文件中读取的数据
      Line=br.readLine();//从文件中继续读取一行数据
     }
     br.close();//关闭BufferedReader对象
     fr.close();//关闭文件
    %>
    </body>
    </html>-----------------------
    就是这样子访问我的t.doc然后在浏览器中打开该文件以word的形式。
    不过中文多是乱码或问号?
    请问怎么显示word中的正确内容,当然还包括图片文字等超连接,在word内部的
      

  2.   

    <%@ include file="taglibs.jsp" %>
    <%@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("download/"+filename)));
              bos=new BufferedOutputStream(response.getOutputStream());
              byte[] buff=new byte[1028];  
              int bytesRead;
              while(-1!=(bytesRead=bis.read(buff,0,buff.length))){
                     bos.write(buff,0,bytesRead);
              }
        }catch(final IOException e){
             e.printStackTrace();
        }finally{
             if(bis!=null)
                  bis.close();
             if(bos!=null)
                  bos.close();
        }
    %>