代码:
java"> // 设置响应头
response.setCharacterEncoding("UTF-8");//设置响应的字符编码为UTF-8
response.setContentType("application/octet-stream");//设置响应的内容类型为二进制流,通常用于文件下载。
response.setHeader("Content-Disposition", "attachment; filename=test.zip");//设置响应头中的Content-Disposition字段,告诉浏览器将响应内容作为附件处理,并指定下载文件的名称为test.zip。
//创建xml文件1
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element test = root.addElement("test");
test.addAttribute("name","淫龙公子");
test.addAttribute("age","38");
test.addAttribute("jjMaxLength","6cm");
Element test1 = root.addElement("test");
test1.addAttribute("name","yi龙公子");
test1.addAttribute("age","66");
test1.addAttribute("jjMaxLength","7cm");
Element test2 = root.addElement("test");
test2.addAttribute("name","王有");
test2.addAttribute("age","77");
test2.addAttribute("jjMaxLength","1cm");
//创建xml文件2
Document document1 = DocumentHelper.createDocument();
Element root1 = document1.addElement("root");
Element test11 = root1.addElement("test");
test11.addAttribute("name","淫龙公子");
test11.addAttribute("age","38");
test11.addAttribute("jjMaxLength","6cm");
Element test12 = root1.addElement("test");
test12.addAttribute("name","yi龙公子");
test12.addAttribute("age","66");
test12.addAttribute("jjMaxLength","7cm");
Element test13 = root1.addElement("test");
test13.addAttribute("name","王有");
test13.addAttribute("age","77");
test13.addAttribute("jjMaxLength","1cm");
//创建xml文件3
Document document2 = DocumentHelper.createDocument();
Element root2 = document2.addElement("root");
Element test21 = root2.addElement("test");
test21.addAttribute("name","淫龙公子");
test21.addAttribute("age","38");
test21.addAttribute("jjMaxLength","6cm");
Element test22 = root2.addElement("test");
test22.addAttribute("name","yi龙公子");
test22.addAttribute("age","66");
test22.addAttribute("jjMaxLength","7cm");
Element test23 = root2.addElement("test");
test23.addAttribute("name","王有");
test23.addAttribute("age","77");
test23.addAttribute("jjMaxLength","1cm");
// 将XML转成字符串,后面能用工具类将字符串转成流
String asXML = document.asXML();
String asXML1 = document1.asXML();
String asXML2 = document2.asXML();
ZipOutputStream out = null;
try {
out = new ZipOutputStream(response.getOutputStream());
//writeToZip:将xml流写到zip输出流中
writeToZip(out,asXML,"test1.xml");
writeToZip(out,asXML1,"test2.xml");
writeToZip(out,asXML2,"test3.xml");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
writeToZip方法:
java">private void writeToZip(ZipOutputStream out,String xmlContent,String fileName) throws IOException {
int len =0;
byte[] buffer = new byte[1024];
InputStream is = IOUtils.toInputStream(xmlContent, "utf-8");
out.putNextEntry(new ZipEntry(fileName));
while ((len = is.read(buffer)) >0){
out.write(buffer,0,len);
}
out.closeEntry();
is.close();
}
注意:前后端分离项目中如果前端有响应拦截器,我们需要再做处理,例如vue项目的下载方法:
javascript">// 通用下载方法
export function download(url, params, filename, config) {
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
return service.post(url, params, {
transformRequest: [(params) => { return tansParams(params) }],
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
responseType: 'blob',
...config
}).then(async (data) => {
const isBlob = blobValidate(data);
if (isBlob) {
const blob = new Blob([data])
saveAs(blob, filename)
} else {
const resText = await data.text();
const rspObj = JSON.parse(resText);
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
Message.error(errMsg);
}
downloadLoadingInstance.close();
}).catch((r) => {
console.error(r)
Message.error('下载文件出现错误,请联系管理员!')
downloadLoadingInstance.close();
})
}
附加(导出xml文件):
java">//导出xml文件
// 设置响应头
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=test.xml");
//创建xml文件
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element test = root.addElement("test");
test.addAttribute("name","淫龙公子");
test.addAttribute("age","38");
test.addAttribute("jjMaxLength","6cm");
Element test1 = root.addElement("test");
test1.addAttribute("name","yi龙公子");
test1.addAttribute("age","66");
test1.addAttribute("jjMaxLength","7cm");
Element test2 = root.addElement("test");
test2.addAttribute("name","王有");
test2.addAttribute("age","77");
test2.addAttribute("jjMaxLength","1cm");
try {
OutputStream outputStream = response.getOutputStream();
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(outputStream, format);
writer.write(document);
writer.close();
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
用到的jar包:
xml"><!-- xml包 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.1</version>
</dependency>