环境: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"的设置,不成功。
请求路过的兄弟,会的话指导一下,当然尽量的说详细些,先此谢过。
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"的设置,不成功。
请求路过的兄弟,会的话指导一下,当然尽量的说详细些,先此谢过。
解决方案 »
- SSH 问题 应该是配置文件出错了 但是找不到哪里出错
- servlet上一个逻辑上的问题。
- Struts2自动跳转的问题
- 求高手帮忙,JSP中怎样获取文本框内的文件路径及文件名!
- 在线求助:关于JSP生成验证码的问题!
- 一个简单的字符处理
- CGLIB Enhancement failed??
- jfreeChart在windows下的中文乱码问题
- 如何插入带单引号的字符串?
- 两个小问题 请求帮忙(各位大哥我没有分了)
- jsp中使用javascript为什么总会出现意料之外的结果
- the selection did not contain anything that can be run on a server
同意上述公约者·申请加入!
QQ群号:25922618
<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里的参数对应
"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就可以打开了
out.clearBuffer(); // 加上这一句
String filename="学习JSP.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>
http://www-128.ibm.com/developerworks/cn/java/l-java-tips/index.html
我也是初学者,说的有什么不对的,请大家指正,多多交流。