我这里用的是SpringBoot,接口将前端的xml文件接收,并传输到service层进行解析,然后保存,保存格式在另一个接口调用,往下看
@RequestMapping(value = "/createHpl", produces = "text/xml;charset=UTF-8")
public void test(HttpServletRequest request, HttpServletResponse response) {
StringBuffer reqXmlData = new StringBuffer();
try {
InputStream inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while ((s = in.readLine()) != null) {
reqXmlData.append(s);
}
//获取hpl内容
String s1 = fileService.prettyPrintByTransformer(reqXmlData.toString(), 1, false);
//保存hpl
boolean b = fileService.saveHpl(s1);
PrintWriter writer = response.getWriter();
writer.print(b);
in.close();
inputStream.close();
} catch (IOException e) {
System.out.println("流解析xml数据异常!");
e.printStackTrace();
}
//判断请求数据是否为空
if (reqXmlData.length() <= 0) {
System.out.println("请求数据为空!");
}
}
这是设置保存文件的格式,包括路径,文件名,扩展名
@RequestMapping(value ="/setFile")
public void setBasePath(HttpServletRequest request,HttpServletResponse response){
//文件路径 win系统 例:”D:“ linux系统 例:“usr/”
String path = request.getParameter("path");
//文件名
String name = request.getParameter("name");
//文件拓展名
String extension = request.getParameter("extension");
boolean b= fileService.setFile(path,name,extension);
try {
response.getWriter().print(b);
} catch (IOException e) {
e.printStackTrace();
}
}
这是完整的service层代码,我实现的是对xml文件的拆分重组,并形成新的文件
package com.example.demo.service.impl;
import com.example.demo.service.FileService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
@Data
@Service
public class FileServiceimpl implements FileService {
//文件名
public static String name;
//文件路径
public static String path;
//文件扩展名
public static String extension;
//保存hpl文件 data hpl内容
public boolean saveHpl(String data){
try {
data=data.replaceAll("<pipeline_configuration>","");
data=data.replaceAll("</pipeline_configuration>","");
data = subRangeString(data, "<pipeline_execution_configuration>", "</pipeline_execution_configuration>");
data = subRangeString(data, "<metastore_json>", "</metastore_json>");
System.out.println(data);
File file = new File(path + "/" + name+"."+extension);
boolean b = file.createNewFile();
if(b) {
Writer out = new FileWriter(file);
out.write(data);
out.flush();
out.close();
}
return b;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
//去除不需要的字段
public String subRangeString(String body,String str1,String str2) {
while (true) {
int index1 = body.indexOf(str1);
if (index1 != -1) {
int index2 = body.indexOf(str2, index1);
if (index2 != -1) {
String str3 = body.substring(0, index1) + body.substring(index2 + str2.length(), body.length());
body = str3;
}else {
return body;
}
}else {
return body;
}
}
}
//格式化xml文件,并为xml加上头
public String prettyPrintByTransformer(String xmlString, int indent, boolean ignoreDeclaration) {
try {
InputSource src = new InputSource(new StringReader(xmlString));
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ignoreDeclaration ? "yes" : "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(out));
return out.toString();
} catch (Exception e) {
throw new RuntimeException("解析xml出错:\n" + xmlString, e);
}
}
@Override
public boolean setFile(String path, String name, String extension) {
FileServiceimpl.path=path;
FileServiceimpl.name=name;
FileServiceimpl.extension=extension;
return true;
}
}