我正在推荐
OutputStream outStream = response.getOutputStream();
在我的jsp文件中打开浏览器中的excel文件。
我得到例外,
java.lang.IllegalStateException: OutputStream already obtained
来源
<%@page import="java.io.File"%>
<%@page import="java.awt.Image"%>
<%@page import="org.jfree.chart.ChartRenderingInfo"%>
<%@page import="org.jfree.chart.entity.StandardEntityCollection"%>
<%@page import="org.jfree.chart.ChartUtilities"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.OutputStream"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.data.general.DefaultPieDataset"%>
<%@page import="java.awt.image.RenderedImage"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Poor", new Double(43.2));
dataset.setValue("Average", new Double(55.2));
dataset.setValue("Good", new Double(4.2));
dataset.setValue("Excellent", new Double(43.2));
JFreeChart chart = ChartFactory.createPieChart("MY CHARTR", dataset, true, true, true);
OutputStream os=response.getOutputStream();
RenderedImage image = chart.createBufferedImage(800, 800);
ImageIO.write(image,"png", os);
%>
</body>
</html>
例外:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
答案 0 :(得分:1)
输出<html>
...标记时,您已经写入输出流。删除html标签,然后离开JSP scriptlet。建议不要在scriptlet标记之间使用换行符