如何使用在浏览器中打开的java下载xml文件?

时间:2019-10-22 06:18:22

标签: java xml browser download vaadin

我正在使用Java和vaadin 14作为框架。我创建了一个xml文件。它创建成功并显示在浏览器中。但是无法自动下载。我下面有一个用于创建和下载该文件的代码。为什么不起作用?

创建新的文件代码-

//Creating a xml file
String xmlOutPath,fname;
Random r = new Random();
fname =   r.nextInt(1000000)+".xml"
File file = File.createTempFile("a-", fname, new File(xmlOutPath));
FileOutputStream fos = new FileOutputStream(file);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

此文件已成功创建。然后我有一个代码下载它-

import com.vaadin.flow.component.UI;
//download xml file
 File downloadFile;
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
fos.close();
downloadFile = file;
UI.getCurrent().getPage().executeJs("location.href = \"./rpttmp/" + downloadFile.getName() + ";\"");

但是我发现这样- https://www.w3schools.com/xml/note.xml

如何自动下载此文件,而不是在浏览器上显示该文件?

0 个答案:

没有答案