解决对前端传递的xml文件进行编辑另存问题

我这里用的是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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两年半的JAVA练习生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值