2013-04-12 54 views
0

我想创建一个XML文件,它将作为下载链接提供给用户(Wicket和TransformerFactory for XSLT)。 目前,我使用File类。但是,我不想在服务器上创建文件。该文件应该只存在于内存中。有人可以告诉我怎样才能做到这一点? 谢谢!内存中的XML文件而不是磁盘上的文件

我当前的代码:

Reader reader = new StringReader("DATA"); 
TransformerFactory tFactory = TransformerFactory.newInstance(); 
Transformer transformer = tFactory.newTransformer(new StreamSource("EXCEL.xsl")); 
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 
File file= new File("FILE.xml");     
transformer.transform(new StreamSource(reader), new StreamResult(new FileOutputStream(file))); 

其他类:

File file = new File("file"); 
add(new DownloadLink("download", file, "XML.xml")); 
+0

就我所知,使用File类,您没有机会完成此操作。 – cgon

回答

1

,你可以用这个启动(不知道这是完全正确的 - 现在不能检查)

ByteArrayOutputStream oStream = new ByteArrayOutputStream(); 
transformer.transform(new StreamSource(reader), new StreamResult(oStream)) 
byte[] xmlResult = oStream.toByteArray(); 
+2

你甚至可能想要考虑直接将流式传输回客户端(在Servlet中使用'response.getOutputStream()',不知道如何访问Wicket中的响应输出流)。 – mthmulders

+0

如果我们已经谈到了一个简单的servlet,我只是建议 –

+1

不要在ByteArrayOutputStream上调用'toString()'。改为调用'toByteArray()'。 –

相关问题