package gov.mof.fasp.ifmis.acal.common.expxml;
import gov.mof.framework.util.DateUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import javax.servlet.http.HttpServletResponse;
import oracle.sql.DATE;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class ExpXmlUtil {
/**
* 构造方法设置为私有.
*/
private ExpXmlUtil() {
super();
}
/**
* 导出凭证报文xml方法.
* @param path --
* @param tmpname
* @param columns
* @param dataList
* @return
* @throws IOException
*/
public static String expExcelTemplate(String path, String tmpname,Document document, HttpServletResponse response) throws IOException {
/*
*第一种处理方式:xml文件保存到服务器
*/
// if (tmpname == null) {
// tmpname = "";
// }
// if (path == null || "".equals(path)) {
// path = new File("").getAbsolutePath();
// path = path.replaceAll("\\\\", "/");
// }
// path += "/cal/impexp/expfilebak/";
// File f = new File(path);
// f.mkdirs();
// String filename = tmpname + ".xml";
// path += filename;
// // 封装xml报文数据
// OutputFormat format = OutputFormat.createPrettyPrint();
// format.setEncoding("utf-8");// 设置XML文件的编码格式
// format.setNewLineAfterDeclaration(false);//声明之后不换行
// //format.setSuppressDeclaration(true);// 不输出头信息
// format.setIndent(false); //设置是否缩进
// //format.setIndent(" "); //以空格方式实现缩进
// format.setNewlines(true); //设置是否换行
//
// XMLWriter writer = null;
// try {
// writer = new XMLWriter(new FileWriter(path), format);//写入指定的文件
// writer.write(document);
// } catch (IOException e) {
// path="";
// e.printStackTrace();
// } finally {
// try {
// writer.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// return path;
/*
*第二种处理方式:返回页面xml格式
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
format.setNewLineAfterDeclaration(false);//声明之后不换行
format.setNewlines(true); //设置是否换行
XMLWriter writer;
try {
writer = new XMLWriter(baos, format);
writer.write(document);
writer.flush();
writer.close();
String fileName = tmpname + DateUtil.getCurrentDateStr(DateUtil.C_DATA_PATTON_YYYYMMDD) + ".xml";
String dispositionHeader = "attachment;filename=" + new String(fileName.getBytes(), "iso8859-1");
// 发送中文文件名的响应头信息
response.setHeader("Content-Disposition", dispositionHeader);
response.setHeader("Content-Type", "application/x-msdownload; charset=gbk");
OutputStream out = response.getOutputStream();
out.write(baos.toByteArray());
out.flush();
out.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*
*第三种处理方式: 同上
*/
// InputStream fis = new ByteArrayInputStream(document.asXML().getBytes("utf-8")) ;
// byte[] buffer = new byte[fis.available()];
// fis.read(buffer);
// fis.close();
// response.reset();
// // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
// response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes(),"iso8859-1"));
// OutputStream out = new BufferedOutputStream(response.getOutputStream());
// response.setContentType("application/octet-stream");
// out.write(buffer);// 输出文件
// out.flush();
// out.close();
return null;
}
/**
* 格式化金额.
* @param amt --金额
* @return
*/
public static String numFormat(String amt) {
DecimalFormat df = new DecimalFormat("#0.00");
BigDecimal amtbd = new BigDecimal(amt);
return df.format(amtbd);
}
}
02.使用dom4j生成xml后, 不同的处理方式
最新推荐文章于 2020-11-30 15:22:35 发布