小弟现在遇到了一个问题,向各位高手跪求解决方案我写了一个下载word文件的jsp,在本地的tomcat能正常下载,也可在线看。放到服务器上后,就出问题了,下载下来的doc打不开,在线也打不开。不知道是哪写错了,源码如下
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%>
<%
//response.reset();
response.setContentType("application/x-download");//设置为下载application/x-download
String realContextPath = request.getSession().getServletContext().getRealPath("/")+"\\download\\招生通知.doc";
String filenamedisplay = "招生通知.doc";
filenamedisplay = new String(filenamedisplay.getBytes("gb2312"),"iso8859-1");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(realContextPath);
byte[] b = new byte[1024];
int i = 0; while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%>
<%
//response.reset();
response.setContentType("application/x-download");//设置为下载application/x-download
String realContextPath = request.getSession().getServletContext().getRealPath("/")+"\\download\\招生通知.doc";
String filenamedisplay = "招生通知.doc";
filenamedisplay = new String(filenamedisplay.getBytes("gb2312"),"iso8859-1");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(realContextPath);
byte[] b = new byte[1024];
int i = 0; while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
呵呵,谢谢你了,把原文件的名字换成英文就好了。
这个是tomcat中文乱码问题吗?怎么解决啊?
String srcpath="";
srcpath=request.getContextPath()+"/download/招生通知.doc";
<a href="<%=srcpath%>" >招生通知.doc</a>文件流的话路径可能有问题,以前也碰到过,路径一直能弄明白,所以就用连接直接下载,这种方式word可以,好像excel就出错,
谢谢你,我看了,我的是
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="GBK" />因为之前lunece搜索时,出现一次乱码,所以这个地方改了。刚才也看了下,自己本地的tomcat反而没加这句话,我加上了试了一下,没什么影响。