- 浏览: 826813 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (530)
- Java编程 (64)
- C/C++/D (6)
- .Net/C# (9)
- Ruby (12)
- JavaScript (77)
- XML (1)
- JSON (1)
- Ajax (17)
- ExtJs (81)
- YUI (1)
- JQuery (7)
- DWR (1)
- HTML (7)
- CSS (7)
- Database (6)
- PowerDesigner (23)
- DB2 (2)
- Oracle (57)
- MS SQL Server (8)
- MySQL (6)
- JSP/Servlet/JSTL/TagLib (3)
- Spring (1)
- Hibernate (0)
- iText (0)
- Struts (0)
- Struts2 (0)
- iReport (0)
- FreeMarker (0)
- HttpClient (1)
- POI (6)
- FckEditor (15)
- Eclipse / MyEclipse (10)
- IntelliJ IDEA (0)
- NetBeans (0)
- Tomcat (11)
- WebLogic (1)
- Jboss (3)
- jetty (4)
- IIS (2)
- CVS/VSS (1)
- FTP (1)
- Windows/DOS (6)
- Linux/Unix (0)
- 软件建模 UML (0)
- Design Pattern & Thinking In Programming (10)
- 数据结构与算法 (12)
- 软件项目管理 (9)
- 行业应用解决方案 (3)
- 电脑软件与故障解决 (13)
- 编程语言 (1)
- 十万个为什么 (3)
- JBPM (2)
- sysbase (2)
- JDBC (8)
- Ant (2)
- Case-计算机辅助软件工程 (1)
- WebService (4)
- 浏览器 (1)
最新评论
-
gaoqiangjava:
同一楼,还请大手帮解决
JAVA读取word文件 -
hyl523:
// 判断数组中的第一个值是否未定义,如果未定义,便定义为空对 ...
javascript面向对象之二 命名空间 -
ping12132200:
ping12132200 写道我抱着个错不是因为:body标签 ...
extjs在IE报对象不支持此属性或方法 -
ping12132200:
我抱着个错不是因为:body标签内的第一个元素不能为文本tex ...
extjs在IE报对象不支持此属性或方法 -
fireinjava:
呀,不错,转走了,谢谢啦~
利用OpenOffice将word转换成PDF
Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException
对于getOutputStream(),api里是这样说的:Either this method or getWriter() may be called to write the body, not both.
也就是getOutputStream()和getWriter() 只能使用一个,把代码里的response.getOutputStream()全部删之后,统一使用response.getWriter() 作为输出流,问题解决。
全部代码如下
response.setContentType( "application/octet-stream ");
response.addHeader("Content-Disposition","attachment;filename=" + "work.xls");
java.io.FileInputStream fileInputStream = null;
ServletOutputStream sos = null;
PrintWriter writer = null;
try
{
fileInputStream = new java.io.FileInputStream(file);
writer = response.getWriter();
response.getOutputStream();
int i;
while((i = fileInputStream.read()) != -1)
{
writer.write(i);
}
writer.flush();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(sos != null)
{
try
{
sos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(fileInputStream != null)
{
try
{
fileInputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(writer != null)
{
writer.close();
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yuzhongzhu/archive/2011/03/31/6290526.aspx#
对于getOutputStream(),api里是这样说的:Either this method or getWriter() may be called to write the body, not both.
也就是getOutputStream()和getWriter() 只能使用一个,把代码里的response.getOutputStream()全部删之后,统一使用response.getWriter() 作为输出流,问题解决。
全部代码如下
response.setContentType( "application/octet-stream ");
response.addHeader("Content-Disposition","attachment;filename=" + "work.xls");
java.io.FileInputStream fileInputStream = null;
ServletOutputStream sos = null;
PrintWriter writer = null;
try
{
fileInputStream = new java.io.FileInputStream(file);
writer = response.getWriter();
response.getOutputStream();
int i;
while((i = fileInputStream.read()) != -1)
{
writer.write(i);
}
writer.flush();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(sos != null)
{
try
{
sos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(fileInputStream != null)
{
try
{
fileInputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(writer != null)
{
writer.close();
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yuzhongzhu/archive/2011/03/31/6290526.aspx#
发表评论
-
java调用dll方法
2011-06-02 21:56 1553Java语言本身具有跨平台性,如果通过Java调用DLL的技术 ... -
DIV+CSS
2011-05-31 13:07 874http://www.divcss5.com/ -
CVS客户端
2011-05-27 14:54 837http://www.syntevo.com/index.ht ... -
UML工具
2011-05-25 18:59 876http://www.umlchina.com/Tools/N ... -
applicationcontext.xml怎么分模块简化配置?
2011-05-22 18:02 2221(1) 在用spring做项止的时候,我们经常会在appli ... -
Struts1.2分模块后的路径问题
2011-05-22 18:00 15871.某项目分模块的web.xml配置如下: <i ... -
java API chm html 1.5 1.6 中文版英文版 帮助文档
2011-05-14 10:45 2649J2SE DK & API下载 ————————- h ... -
ClientAbortException原因探究
2010-12-23 16:17 1840WEB是部署在TOMCAT5.5.17下面的,采用JNDI链接 ... -
java游戏 http://blog.csdn.net/cping1982/archive/2009/06/10/4258704.aspx
2010-11-08 22:31 1206http://blog.csdn.net/cping1982/ ... -
Java中字符串(String)的存储和赋值原理
2010-11-08 22:14 1203可能很多java的初学者对String的存储和赋值有迷惑, ... -
Java中字符串的最大长度
2010-11-08 22:12 1285在cpp中为了可移植性,string的长度是string::s ... -
Java与XML联合编程之DOM篇
2010-10-06 13:37 861一、DOM初步 DOM是Document Object ... -
jexl解析表达式字符串
2010-10-06 11:44 1928网址:http://commons.apache.org/je ... -
Java中getResourceAsStream的用法
2010-10-06 11:31 1979Java中getResourceAsStream的用法 首先 ... -
Class.getResourceAsStream 和 ClassLoader.getResourceAsStream
2010-10-06 11:08 826Class.getResourceAsStream 和 Cla ... -
利用第三方的Jar包内的类和方法来判别文件编码
2010-08-06 13:07 1745今天在论坛里看见了一个人发帖子问,如何查看文件的编码。有一个人 ... -
将Java程序作成exe文件的几种方法
2010-08-06 13:04 945看到网上有同志的介绍将Java程序作成exe文件的方法,写的不 ... -
Java基本类型转换总结
2010-07-26 19:49 1359数值型转换成字符型 // 基本数据类型, int i_a ... -
怎样不使用中间变量来交换两个变量的值?
2010-07-10 21:39 1443int 的话就+后再减.string 的话就连接再截取. ... -
使用Java Service Wrapper 把Java程序作为Windows系统服务
2010-07-09 18:00 834转自:http://www.cjsdn.net/post/vi ...
相关推荐
从excel导入数据到datawindow-转载
NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2336132
本节主要介绍了response.getWriter().write()向前台打印信息乱码问题解决方法,需要的朋友可以参考下
Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to ...
* This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the ...
JavaWeb教材配套资源,内含每个章节的Demo项目源码。包含教材最后完整的网上商城案例源码,包含sql及项目源码... PrintWriter out = response.getWriter(); out.print("this servlet is created by eclipse"); } }
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...
PrintWriter out = response.getWriter(); out.println(你好,世界!); 前端显示: 1.2 原因 没有设置HttpServletResponse使用哪种编码,默认编码跟浏览器解码不匹配。 1.3 解决方案: 方案1: 在PrintWriter out ...
* This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to ...
Servlet + Tomcat 中文乱码的解决方法,浏览器把Unicode字符转换为GBK字符,这样页面的内容和浏览器的显示模式都设成了GBK,就不会乱码了
java.io.PrintWriter out = response.getWriter(); for(int i= 0;i;i++)//循环显示每个分类 out.println(treeviewRender.renderTreeViewAjax(top[i],false)); } } public void doGet...
* This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server ...
PrintWriter out = response.getWriter(); AdvertDao adverDao=new AdvertDao(); List<Advert> list=adverDao.getAdverts(); out.write(JSONArray.fromObject(list).toString());//把数据转换成...
getWriter() 和Response.getOutputStream冲突
PrintWriter getWriter() 获得字符流,通过字符流的write(String s)方法可以将字符串设置到response 缓冲区中,随后Tomcat会将response缓冲区中的内容组装成Http响应返回给浏览 器端。 文件下载的实质就是文件拷贝...