环境:windows2003+tomcat5.5+jdk1.5.0_01
test_12.jsp:
  <%@ page contentType="text/html;charset=gb2312" %>
  <%@ page language="java" %>
  <%@ page import="java.text.*" %>   
  <%@ page import="java.util.*" %>   
  <%@ page import="java.io.*" %>
  <html>
  <head>
  <title>
  test_12
  </title>
  </head>
  <body>
<%
String filename="学习JSP.doc";
String filepath="D:/";// 设置响应头和下载保存的文件名
response.setContentType("application/msword;charset=gb2312");
response.setHeader("Content-Disposition",
"inline;filename=\""+filename+"\"");// 打开指定文件的流信息
java.io.FileInputStream fileInputStream =
new java.io.FileInputStream(filepath + filename);// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1)
{
 out.write(i);
}
fileInputStream.close();
out.close();
%>
</body>
</html>
现象:IE打开页面后能够正常在ie里面使用word程序,但是显示的内容均为乱码(比乱马1/2还要乱码,呵呵)。
自己的尝试:1.根据网上查资料,试图在web.xml添加代码,不成功;
2.多次尝试修改contentType="text/html;charset=gb2312"的设置,不成功。
请求路过的兄弟,会的话指导一下,当然尽量的说详细些,先此谢过。

解决方案 »

  1.   

    word应该有它自己的数据结构,当你打开的时候,其实并不是ie打开的,而是word程序,所以我感觉和你的charset=gb2312没有什么关系才对,只是我的感觉啊
      

  2.   

    Java 技术交流群!入群先看公约!本群公约:互助、互勉、共同进步!惑则问、知则答、不知则表示关注。帮助新手、细心回答。 
    同意上述公约者·申请加入!  
    QQ群号:25922618
      

  3.   

    去掉所有的html标签和jsp代码段外,所有字符试试
      

  4.   

    <action name="download2" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
                <param name="inputPath">/images/struts-gif.zip</param>
                <result name="success" type="stream">
                    <param name="contentType">application/zip</param>
                    <param name="inputName">inputStream</param>
                    <param name="contentDisposition">filename="struts-gif.zip"</param>
                    <param name="bufferSize">4096</param>
                </result>
            </action>protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {        OutputStream oOutput = null;        try {
                if (inputStream == null) {
                    // Find the inputstream from the invocation variable stack
                    inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));            }            if (inputStream == null) {
                    String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
                        "Check the <param name=\"inputName\"> tag specified for this action.");
                    log.error(msg);
                    throw new IllegalArgumentException(msg);
                }            // Find the Response in context
                HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);            // Set the content type
                oResponse.setContentType(conditionalParse(contentType, invocation));
                // Set the content length
                if (contentLength != null) {
                    String _contentLength = conditionalParse(contentLength, invocation);
                    int _contentLengthAsInt = -1;
                    try {
                        _contentLengthAsInt = Integer.parseInt(_contentLength);
                        if (_contentLengthAsInt >= 0) {
                            oResponse.setContentLength(_contentLengthAsInt);
                        }
                    }
                    catch(NumberFormatException e) {
                        log.warn("failed to recongnize "+_contentLength+" as a number, contentLength header will not be set", e);
                    }
                }            // Set the content-disposition
                if (contentDisposition != null) {
                    oResponse.addHeader("Content-disposition", conditionalParse(contentDisposition, invocation));
                }            // Get the outputstream
                oOutput = oResponse.getOutputStream();            if (log.isDebugEnabled()) {
                    log.debug("Streaming result [" + inputName + "] type=[" + contentType + "] length=[" + contentLength +
                        "] content-disposition=[" + contentDisposition + "]");
                }            // Copy input to output
                log.debug("Streaming to output buffer +++ START +++");
                byte[] oBuff = new byte[bufferSize];
                int iSize;
                while (-1 != (iSize = inputStream.read(oBuff))) {
                    oOutput.write(oBuff, 0, iSize);
                }
                log.debug("Streaming to output buffer +++ END +++");            // Flush
                oOutput.flush();
            }
            finally {
                if (inputStream != null) inputStream.close();
                if (oOutput != null) oOutput.close();
            }
        }
    这是struts2里对文件下载作的处理,我简化以下,用一个servlet来处理,ok的 下边的java代码和上边的xml里的参数对应
      

  5.   

    OutputStream oOutput = null; try { InputStream inputStream = new FileInputStream(new File(
    "D:/Myeclipse6/StrutsDemo/WebRoot/aa.doc")); response.setContentType("application/msword;charset=gb2312");
    response.addHeader("Content-disposition", "filename=aa.doc"); oOutput = response.getOutputStream();
    byte[] oBuff = new byte[4096];
    int iSize;
    while (-1 != (iSize = inputStream.read(oBuff))) {
    oOutput.write(oBuff, 0, iSize);
    } oOutput.flush();
    } catch (Exception e) {
    e.printStackTrace();
    }
    这是我抽取出来的,用户请求这个servlet就可以打开了
      

  6.   

    <% 
    out.clearBuffer(); // 加上这一句
    String filename="学习JSP.doc"; 
      

  7.   

    你可以直接打开文件,word.doc存放在当前路径
    <HTML>
    <HEAD>
    <TITLE>open word</TITLE>
    </HEAD>
    <BODY>
    <a href="./word.doc">open the word</a>
    </BODY>
    </HTML>
    在web.xml里面加上下面两句:
        <mime-mapping>
            <extension>doc</extension>
            <mime-type>application/vnd.ms-word</mime-type>
        </mime-mapping>
        
        <mime-mapping>
            <extension>xls</extension>
            <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
      

  8.   

    很感谢楼上所有同学的帮助,回去认真研究过大家提供的代码,再加上网上搜索到的信息,总结了一下:用JSP读取文件的话,对于简单的文本文件,例如txt啊,sql啊等等,均是可以的,办法也很简单,我1楼给的例子就可以,但是遇到文件内容有中文的话,显示乱码,那就需要在读取的时候指定编码这样才可以,而不是读取后显示的时候进行转换;但是对于word、excel等内含丰富的控制符的文件的话,要想用jsp直接读取然后显示给客户端,我还没有找到办法,因此只能认为是奢望,会一股脑儿的将文档作为对象解剖给你看,那怎么办?给大家一个连接,希望能够给有需要的同学学习一下。
    http://www-128.ibm.com/developerworks/cn/java/l-java-tips/index.html
    我也是初学者,说的有什么不对的,请大家指正,多多交流。