站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题请文在jsp页面中如何下载一个中文名字的word文档?

请文在jsp页面中如何下载一个中文名字的word文档?

摘要: 请文在jsp页面中如何下载一个中文名字的word文档?


以前试过用流的办法,但是好像因为getoutputstream和jsp页面的out流冲突,造成getoutputstream has been called 这个异常,谁有好办法解决嘛?不要求办法,只要实现功能。这个问题困扰我好几天了,请各位大哥帮帮忙啊


http://community.csdn.net/Expert/topic/4969/4969237.xml?temp=.7287714


这是重复问题


<%%>之外不要有空格和换行


我早试过了,我把整个jsp写得只有一行还没有空格也不行啊,大哥,你们说的那办法到底有用没啊?


我知道这是重复问题,但是我看网上还没有解决过的例子啊,好多人说不用空格不要换行什么的,但是我不但没有空格和换行,在最后一行用了 out.clear,这样应该调用不到out输出流了吧?可是问题依旧啊,大家帮忙看看啊。这个问题很多天了,都没解决


把代码贴出来,让大家看看


<%String filename=new String(URLDecoder.decode(file),"ISO-8859-1");
BufferedInputStream bis=null;
BufferedOutPutStream bos=null;
try{response.reset();
response.setContentType("Application/msword;charset='UTF-8'");
response.addHeader("Content-disposition","attachment;filename="+new String (filename.getBytes("gb2312"),"ISO-8859-1"));
bis=new BufferedInputStream(new FileInputStream(getServletContext().getRealPath(""+filename)));
bos=new BufferedoutPutstream(response.getoutputStream());
int BytesRead;
byte[] buff=new byte[2048];
while (-1!=(bytesRead=bis.read(buff,0,buff.length)))
bos.write(buff,0,bytesRead);
catch(final exception e)
{System.out.println(e);}
finally
{if (bis!=null) bis.close;
if (bos!=null bos.close;) }}
%>



<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="GBK"%><%
String filename=new String(URLDecoder.decode(file),"ISO-8859-1");
BufferedInputStream bis=null;
BufferedOutPutStream bos=null;
try{response.reset();
response.setContentType("application/x-download;charset='UTF-8'");
response.addHeader("Content-disposition","attachment;filename="+new String (filename.getBytes("gb2312"),"ISO-8859-1"));
bis=new BufferedInputStream(new FileInputStream(getServletContext().getRealPath(""+filename)));
bos=response.getoutputStream();
int BytesRead=0;
byte[] buff=new byte[2048];
while (-1!=(bytesRead=bis.read(buff,0,buff.length)))
bos.write(buff,0,bytesRead);
catch(final exception e)
{ e.printStackTrace();}
finally
{if (bis!=null) bis.close;
if (bos!=null bos.close;) }}
%>
现在你试一试?


An error occurred at line: 1 in the jsp file: /Docdown.jsp
Generated servlet error:
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete Expression

An error occurred at line: 1 in the jsp file: /Docdown.jsp
Generated servlet error:
Syntax error, insert ") Statement" to complete IfStatement

An error occurred at line: 1 in the jsp file: /Docdown.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)

An error occurred at line: 1 in the jsp file: /Docdown.jsp
Generated servlet error:
Syntax error on token ")", delete this token



大哥很感谢你这么热心回答我的问题,但是为什么这么多错啊?我检查了半天是对的啊?


我已经找到解决办法,不过不是很明白,贴出来给大家看看,顺便讨论讨论
<%@page language="java" import="java.net.*" pageEncoding="gb2312"%>
<%response.setContentType("application/x-download");
String filenamedownload="/帮助文档.doc";
String filedisplayname="帮助文档";
filedisplayname=URLEncoder.encode(filedisplayname,"UTF-8");
response.addHeader("Content-Disposition","attachment,filename="+filedisplayname+")";
try{requestDispatcher dispatcher=application.getRequestDispatcher(filenamedownload);
if (dispatcher!=null)
{dispatcher.foward(request,response();)}
response.flushBuffer();}
catch(Exception e)
{e.printStack;}
finaly{}




<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="GBK"%><%
String filename=new String(URLDecoder.decode(file),"ISO-8859-1");//我不知道你是如何获取文件名的,所以可能存在编码问题,你最好先用个英文的文件来试验,然后用中文的试一试
BufferedInputStream bis=null;
BufferedOutPutStream bos=null;
try{response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.addHeader("Content-disposition","attachment;filename=/""+new String (filename.getBytes("gb2312"),"ISO-8859-1")+"/"");//这个地方你原来的代码也有错
bis=new BufferedInputStream(new FileInputStream(getServletContext().getRealPath(""+filename)));
bos=response.getoutputStream();
int BytesRead=0;
byte[] buff=new byte[2048];
while ((bytesRead=bis.read(buff,0,buff.length))>0)
bos.write(buff,0,bytesRead);
}
catch(final exception e)
{ e.printStackTrace();}
finally
{if (bis!=null) bis.close;
if (bos!=null) bos.close; //原来的代码,这个地方有错
}
%>
你再试一试改过的代码


大哥你真有耐心,我是学自动化专业的,因为兴趣进入一个软件公司,现在还在试用,有很多地方需要朋友的帮助,能同我交个朋友嘛?QQ:78329897


↑返回目录
前一篇: 五十分请高手,这事该咋办,事关饭碗
后一篇: 为什么我在数据库查询分析器可以运行可是在程序里提示是 [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第 1 行: '.' 附近有语