我想从 xml 文件打开流,然后在 jsp 文件中使用 xsl 转换。一切似乎都是正确的,但我不知道为什么当我从响应中获取输出时会出现异常。
这是我的代码
<%@page import="javax.xml.transform.*" %>
<%@page import="javax.xml.transform.stream.*" %>
<%@page import="java.io.*" %>
<%
StreamSource xmlSource = new StreamSource( new File(application.getRealPath("foo/cd.xml")));
StreamSource xsltSource = new StreamSource( new File(application.getRealPath("foo/cd.xsl")));
StreamResult fileResult = new StreamResult(response.getOutputStream());
try {
// Load a Transformer object and perform the transformation
TransformerFactory tfFactory = TransformerFactory.newInstance();
Transformer tf = tfFactory.newTransformer(xsltSource);
tf.transform(xmlSource, fileResult);
} catch(TransformerException e) {
throw new ServletException("Transforming XML failed.", e);
}
%>
例外是:java.lang.IllegalStateException: getOutputStream() 已经为此响应调用
那么我该如何摆脱它。谢谢