解析xml入库

先在盘符下创建xml文件存储路径,然后在创建一个数据导入玩之后剪贴到一个新的xml路径里。在创建xml文件,之后在config.xml里配置自己写的路径。

xml文件:

这是xml格式,是和数据库字段对应的。

<?xml version="1.0"?>

<class>
    <student></student>
    <time></time>
    <id></id>
</class>

  config.xml:

<reacvexmlPathw>C:\test\xml</reacvexmlPathw>
    <!--    解析后xml剪切路径-->
<copyreacvexmlPathw>C:\test\xx</copyreacvexmlPathw>

定义实体类

//实体类
@Data
@EqualsAndHashCode(callSuper = false)
public class Test{
    //学生
    private String student;
    //学号
    private Long id;
    //时间
    private Timestamp time;

}

Mapper层:

public interface TestMapper{
    //这里是基于mybatispubls开发,所有没有写sql语句,sql语句就是插入,不用mybatispubls的自己写下sql语句。
    //int insert (Test test); 如这样 sql在xml里实现
}

service层:

public interface TestService{
    //获取文件夹下所有的文件的路径
    List<File> getFiles(String path);
    //根据路径读取文件内容
    String toArrayByFileReader(String name);
    public boolean xml(String feil);
    
}

serviceImpl:

@Service
@Slf4j
public class TestServiceImpl extends thread implements TestService{
    public TestServiceImpl(TestMapper testmapper){
        this.testmapper=testmapper;
    }

    @Autowired
    TestMapper testmapper;
    //这里采用多线程来实现
       
    //获取该文件下的所有文件的绝对路径
    public List<File> getFiless(String path) {
        File root = new File(path);
        ArrayList<File> files = new ArrayList<>();
        //判断是否为空
        if (!root.isDirectory()){
            //就创建
            files.add(root);
        }else {
            File[] subFiles= root.listFiles();
            for (File f : subFiles){
                //读取文件并存放到另一个文件夹
                files.addAll(getFiless(f.getAbsolutePath()));
            }
        }

        return files;
    }

        //通过路径读取文件内容
    public String toArrayByFileReader(String name) {
        //使用ArrayList来存储每行读取到的字符串
        StringBuilder s = new StringBuilder();
        String txt = "";
        try {
            //读取文件FileReader
            FileReader fr = new FileReader(name);
            //缓冲区读取内容,避免中文乱码
            BufferedReader bf = new BufferedReader(fr);
            String str;
            //按行读取字符串 bf. readLine()方法是阻塞式的,如果到达流末尾,就返回null
            while ((str = bf.readLine()) !=null){
                //append()是往动态字符串数组添加
                s.append(str);
            }
            txt= s.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return txt;
    }

    @Override
    public boolean xml(String feil) {
        return false;
    }
    public int ARCHEXPNOTIFY(String dataXml){
        Test test = new Test();
        System.out.println(dataXml);
        //解析接收的xml文件
        XMLTool xmlTool = new XMLTool();
        //定义map
        Map<String,Object> map = xmlTool.createXML(dataXml);
        Timestamp time= null;
        try {
            //时间转换toString格式
            time=new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("time").toString()).getTime());
         } catch (ParseException e) {
            e.printStackTrace();
        }
        //存放到map集合
        test.setTime(time);
        test.setStudent(map.get("Student").toString());
        test.setId((Long) map.get("id"));   
        System.out.println(test.toString());
        //调用insert sql方法
        int a= testmapper.insert(test);
        System.out.println("qqqqqqqqqqqqq"+a+"aaaaaaaaaaaaaaaaaaaaaa");
        return a;
    }

        @Override
    public  void  run() {
        //配解析文件的路径
        String xmlPath = Propertie.key("reacvexmlPathw");
        //获取该文件系的所有文件的绝对路径
        File reacvexmlPath = new File(xmlPath);
        //判断是否存在
        if (!reacvexmlPath.exists()) {
            //不存在创建
            reacvexmlPath.mkdir();
        }
        List<File> files = getFiless(xmlPath);
        for (File f : files) {
            String xml = toArrayByFileReader(f.toString());
            //解析接收的xml文件
            int a = ARCHEXPNOTIFY(xml);
            if (a > 0) {
                //定义输出和输入流
                FileInputStream inputStream = null;
                FileOutputStream outputStream = null;
                try {
                    //创建输入流
                    inputStream = new FileInputStream(f);
                    //从参数中获取地址
                    String fileName = f.toString().substring(f.toString().lastIndexOf(File.separator));
                    //读取配置文件的值   存放到新的地址
                    String copyxmlPath = Propertie.key("copyreacvexmlPathw");
                    File o = new File(copyxmlPath);
                    File out = new File(copyxmlPath + fileName);
                    if (!o.exists()) {
                        o.mkdirs();
                    }
                    if (!out.exists()) {
                        //创建临时文件夹
                        out.createNewFile();
                    }
                    outputStream = new FileOutputStream(out);
                    //定义一个字节数组,相当于缓存
                    byte[] bytes = new byte[100];
                    int count = 0;//得到实际读取到的字节数,读取到最后返回-1
                    //只要没读完就继续读
                    while ((count = inputStream.read(bytes)) != -1) {//把fis里的东西读到bytes数组中
                        //每次写
                        outputStream.write(bytes, 0, count);
                        outputStream.flush();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        try {
                            //关流
                            inputStream.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                boolean result = false;
                int tryCount = 0;
                while (!result && tryCount++ < 10) {
                    System.gc();
                    //把之前的删除
                    result = f.delete();
                }
                System.out.println(result);
            }
            System.out.println("1111111111111111111111111");
        }
    }
        
}

controller层:

@RestController
@RequestMapping("/mbbzPlan")
@Slf4j
public class TestController {
      
    @Autowired
    TestService testservice;
    @Autowired
    private TestMapper testMapper;



    @Scheduled(cron = "59 * * * * ?")
    public void getFiles(){
        TestServiceImpl wh = new TestServiceImpl(testMapper);
        //调用多线程
        wh.run();
        System.out.println("定时器已启动");
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值