/**
* 导出xml文件
* @param picDir
* picDir
* @param fileName
* fileName
* @param list
* list
* @param names
* names
* @param response
* response
*/
@SuppressWarnings("unchecked")
public static void writerXml(File picDir,String fileName,List<Map> list,
List<String> names,HttpServletResponse response)
{
BufferedWriter bw = null;
try
{
FileWriter fw = new FileWriter(fileName);//用来写入字符文件的便捷类
bw = new BufferedWriter(fw);//包装一下
StringBuffer str = new StringBuffer(""); //用来存储xml字符串的
str.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n");
str.append("<root>\n");
String[] title = new String[]{"监测终端编号:(6位字母或数字 必填", "监测终端类型:(必填)", " 监测终端型号:(必填)", " SIM卡号:", "生产厂家:"
,"出厂日期:(日期格式 'yyyy-mm-dd'例如2013-10-12)"
,"出厂编号:","终端密码:","终端投运日期:(日期格式 'yyyy-mm-dd'例如2013-10-12"
,"终端状态:(0或1)","终端经度:(-180.00000~180.00000 必填)"
,"终端纬度:(-90.00000~90.00000 必填)","分局:","区域:(东南西北中或者空)","数据同步主站(0或1):","安装位置:","(导地线拉力及倾角监测功能22H),"
+"(绝缘子泄漏电流监测功能24H),(气象数据监测功能25H),(导线温度、电流数据监测功能26H),(杆塔振动数据监测功能27H),(导线侧倾角监测功能28H),"
+"(舞动振幅频率监测功能29H),(杆塔倾斜数据监测功能2AH),(导线微风震动数据监测功能2BH),(综合防盗功能2CH),(山火报警功能2DH),(大风舞动报警功能2EH)"
+",(设备故障自检功能30H),(微风振动动态数据监测功能32H),(舞动动态数据监测功能36H),(图像监测功能84H)"
};
for (int i = 0; i < list.size(); i++)
{
str.append("\t<row id=\"" + (i + 1) + "\">\n");
Map map = new HashMap();
map = list.get(i);
for (int j = 0; j < names.size(); j++)
{
String name = names.get(j);
String value = (String) map.get(name);
str.append("<!-- "+title[j]+"-->\n");
if (value != null && value.length() > 0)
{
str.append("\t\t<" + name + ">" + value + "</" + name
+ ">\n");
}
else
{
str.append("\t\t<" + name + ">"+"</" + name + ">\n");
}
}
str.append("\t</row>\n");
}
str.append("</root>");
bw.write(str.toString());//写出到文件
bw.flush(); //刷新输出流
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
bw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
FileInputStream fis = new FileInputStream(fileName);
java.io.OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b))!=-1)
{
os.write(b, 0, len);
}
os.flush();
os.close();
fis.close();
}
catch (Exception e)
{
return;
}
deleteFile(picDir);
}
/**
* 导出xml
* @param request
* request
* @param response
* response
* @throws IOException
*/
@SuppressWarnings("unchecked")
@RequestMapping(params = "act=writerXml1")
public void writerXml1(HttpServletRequest request,HttpServletResponse response) throws IOException
{
//调用写出xml
File picDir=null;
String path = request.getSession().getServletContext().getRealPath("/testXml/");
path=path.replace("/", File.separator);
String filePath = path;
picDir = new File(filePath);
if (!picDir.exists())
{
picDir.mkdir();
}
File pictureFile = new File(filePath + File.separator + "netInformation.xml");
if (!pictureFile.exists())
{
pictureFile.createNewFile();
}
String path1 = pictureFile+"";
response.setHeader("Content-disposition", "attachment;fileName=netInformation"
+ ".xml");
response.setContentType("application/x-tar");
//定义一个存放数据的List
List<String> names = new ArrayList<String>();
names.add("bsId");
names.add("bsType");
names.add("bsModal");
names.add("bsSim");
names.add("bsManufacturer");
names.add("bsLeaveFactoryDay");
names.add("bsIndentifier");
names.add("bsPassword");
names.add("bsServiceDate");
names.add("bsStatus");
names.add("longitude");
names.add("latitude");
names.add("bureau");
names.add("area");
names.add("bsNeedUpload");
names.add("remarks");
names.add("functions");
List<Map> list = new ArrayList<Map>();
Map map = new HashMap();
map.put("bsId", "");
map.put("bsType", "");
map.put("bsModal", "");
map.put("bsSim", "");
map.put("bsManufacturer", "");
map.put("bsLeaveFactoryDay", "");
map.put("bsIndentifier", "");
map.put("bsPassword", "");
map.put("bsServiceDate", "");
map.put("bsStatus", "");
map.put("longitude", "");
map.put("latitude", "");
map.put("bureau", "");
map.put("area", "");
map.put("bsNeedUpload", "");
map.put("remarks", "");
map.put("functions", "");
list.add(map);
//把文件名,list集合,数据的列名做参数,
writerXml(picDir,path1,list, names,response);
logger.debug("导出成功------");
}
private static void deleteFile(File file)
{
if (file.exists())
{ //判断文件是否存在
if (file.isFile())
{ //判断是否是文件
file.delete(); //delete()方法 你应该知道 是删除的意思;
}
else if (file.isDirectory())
{ //否则如果它是一个目录
File files[] = file.listFiles(); //声明目录下所有的文件 files[];
for (int i=0;i<files.length;i++)
{ //遍历目录下所有的文件
//System.out.println(files[i]);
deleteFile(files[i]); //把每个文件 用这个方法进行迭代
}
}
file.delete();
}
else
{
logger.debug("所删除的文件不存在!");
}
}