试试response.setContentType("application/msword;charset=gb2312")

解决方案 »

  1.   

    <%@ page contentType="text/html; charset=GB2312"%>
    这句话我也加上去了,没有用的。
      

  2.   

    // Set the browser's mime type
    response.setContentType("application/msword");response.addHeader("Content-disposition", "inline; filename=myword.doc");
      

  3.   

    这样试试,download.jsp:
    <%
    java.io.FileInputStream fis = new java.io.FileInputStream("D:\test.doc");
    byte[] data = new byte[fis.available()];
    fis.read(data);
    fis.close();
    response.reset();
    response.setContentType("application/msword;charset=gb2312");
    response.getOutputStream().write(data);
    %>注意:<%前不要有任何内容,%>后也不要有任何内容,换行也不要有
      

  4.   

    谢谢楼上各位,情况是这个,这个JSP报表页面中有七个循环,若修改成只有三个循环时(其它不动)即可正常打印,多于三个循环时,就乱码,你们遇见过这种情况吗?这问题还没有解决啊
      

  5.   

    前言:用的是struts架构,这个JSP中由于后面一段数据较复杂,用struts标签无法实现,于是嵌入了一段代码。
    <%@ page language="java" %>
    <%@ page contentType="text/html; charset=gb2312"%>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/tlds/taglib.tld" prefix="windy"%>
    <%@ page import="java.util.*"%>
    <%@ page import="cn.com.gever.ewater.sanfang.sw.action.StationWaterLevelForm"%>
    <%@ page import="cn.com.gever.ewater.sanfang.sw.objectvalue.StationQueryView"%>
    <%
    response.setContentType("application/msword");
    %>
    <html:errors/><html:form method="POST"  action="/sanfang/sw/stationlist">
    <div align="left">
     <table width="100%" border="0" cellpadding="3" cellspacing="1">
              <tr>            <td align="center" class="t18"><font color="#003399"><bean:write name="StationWaterLevelForm" property="sdate" filter="true"/>
                  各江主要站点水位</font></td>
              </tr>
            </table>      <br>
            <table border="1" cellspacing="0" cellpadding="0" width="100%" align="center"  bordercolor="#000000" style='width:100.0%;
     border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
     mso-padding-alt:0cm 0cm 0cm 0cm'>
              <tr align="center"  class="t12">
                
          <td width="13%">站名</td>
                <windy:extenditerate id="element" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" pageCount="200">
                <td ><bean:write name="element" property="FZM" filter="true"/></td>
                </windy:extenditerate> </tr>
              <tr bgcolor="#FFFFFF" class="t12">
                
          <td height="22">预警戒水位</td>
                <windy:extenditerate id="element" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" >
                <td ><bean:write name="element" property="FYJJSW" filter="true"/></td>
                </windy:extenditerate> </tr>
              <tr bgcolor="#FFFFFF" class="t12" >
                <td height="22">警戒水位</td>
                <windy:extenditerate id="element1" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" >
                <td ><bean:write name="element1" property="FJJSW" filter="true"/></td>
                </windy:extenditerate> </tr>
              <tr bgcolor="#FFFFFF" class="t12">
                <td height="22">历年最高水位</td>
                <windy:extenditerate id="element3" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" >
                <td ><bean:write name="element3" property="FZGSW" filter="true"/></td>
                </windy:extenditerate> </tr>
              <tr bgcolor="#FFFFFF" class="t12" >
                <td height="22">50年最高水位</td>
                <windy:extenditerate id="element4" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" >
                <td ><bean:write name="element4" property="FWSW" filter="true"/></td>
                </windy:extenditerate> </tr>
              <tr bgcolor="#FFFFFF" class="t12">
                <td height="22">当日八时雨量</td>
                <windy:extenditerate id="element5" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" >
                <td ><bean:write name="element5" property="FRAIL" filter="true"/></td>
                </windy:extenditerate> </tr>
              <%

    StationWaterLevelForm form = (StationWaterLevelForm)session.getAttribute("StationWaterLevelForm");

    ArrayList levelList = (ArrayList)form.getMyLevelList();//水位信息
    ArrayList timeList = (ArrayList)form.getMyTimeList();//时间信息 int count = form.getFZM_count(); for(int m=0;m<timeList.size();m++){                                %>
              <tr bgcolor="#FFFFFF" class="t12" >
                <td height="22"><%=((StationQueryView)timeList.get(m)).getFTIME()%></td>
                <%
                                       for(int n=m*count;n<(m+1)*count;n++){
                                       %>
                <td ><%=((StationQueryView)levelList.get(n)).getFSW()%></td>
                <%}%>
              </tr>
              <%
       }
    %>
            </table></div>
      </html:form>
      

  6.   

    代码运行没问题,
    没有<%response.setContentType("application/msword");%> 的话运行正常(无乱码),<windy:extenditerate id="element5" name="StationWaterLevelForm" property="paginationForm.list" indexes="indexes"  pagination="paginationForm.pagination" > 为循环标签,当循环少于四个时运行也无乱码,我该如何解决啊?
      

  7.   

    这两行有冲突:
    1。 <%@ page contentType="text/html; charset=gb2312"%>2。 response.setContentType("application/msword");行1将charset设置为gb2312,行2又去掉了该设置。所以你返回的内容没有charset属性。将他们合并为
        <%@ page contentType="application/msword; charset=gb2312"%>
    试试!
      

  8.   

    加上<meta http-equiv=Content-Type content="text/html; charset=GB2312">,如下:
    <%@page contentType="application/msword;charset=GB2312"%><html>
    <head>
    <meta http-equiv=Content-Type content="text/html; charset=GB2312">
    </head>
    <body>
    <form method="POST"  action="/sanfang/sw/stationlist">
    ...
    </form>
    <body>
    </html>
      

  9.   

    多谢楼上的提醒,我由于使用了模板才造成乱码,改成标准JSP即可,问题已解决。各位说得都没错,谢谢cbhyk,谢谢大家!