解决方案 »
- 关于FusionCharts的参数问题
- 急:明天上班要用.简单的问题.
- spring报表中文化
- 统计下,谢谢各位给我们新手提供第一手资料
- weblogic+jb瓜瓜问题
- 关于struts中的中文问题,急啊,请大家帮忙看下!
- sqlserver jdbc 驱动程序哪里可以下载(立即给分!!!)
- [struts]问struts中标签<bean:message>什么意思
- class.forName(): Syntax error on token "(", "Identifier"请大哥帮忙看看啊~~~~~
- 关于CMP的relationship
- 关于应用memcached作为缓存的几个问题
- hibernate hql查询问题
如果是数据问题,就不会是getOutputStream() has already been called for this response
这个异常了!
返回null,是没有错了,但我的问题是怎样弹出那个下载的对话框
地址:http://blog.csdn.net/zhuli_java 或QQ359459774留言,谢谢!!!
public static HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
//response.setHeader("Content-disposition","attachment;filename="+"book.zip");
response.addHeader("Content-Disposition", "inline;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/OCTET-STREAM;charset=gb2312");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
}
调用方法:
public class EmployeeAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
ActionContext context=ActionContext.getContext();
HttpServletRequest request ;
HttpServletResponse response ;
public HttpServletResponse getDownloadFile(){
String str="/Excel/empInfo.xls";
String path = request.getSession().getServletContext().getRealPath(str);
return DownloadFiles.download(path, response);
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
}