jtidy的应用以及读取clASSES下的文件

本文介绍了一种用于格式化HTML文件的工具Jtidy,并提供了使用Spring框架下的ClassPathResource来加载配置文件的具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jtidy 的应用以及读取 clASSES 下的文件(这里用到的是 spring ClassPathResource ),我把 jtidy 的配置文件放在了 web-inf/classes/jtidyConfig.properites

    /**
     * 将html文件格式化,以便正常显示
     * @author linshutao
     * @param f 输入的文件
     * @return File 优化完的文件
     */

    public static File cleanByJtidy(File f){

       Tidy tidy = new Tidy();

       ClassPathResource resource = new ClassPathResource(

              "jtidyConfig.properties" );

       String jtidyConfigFilepath = "" ;

       String cleanedFileName = "" ;

       try {

           jtidyConfigFilepath = resource.getURL().getPath()

                  .replace( "20%" , "" );

           tidy.setConfigurationFromFile(jtidyConfigFilepath);

           FileInputStream fis = new FileInputStream(f);

           String sourceFileName = f.getAbsolutePath();

           System. out .println( "############sourceFileName:" + sourceFileName);

           cleanedFileName = sourceFileName.substring(0, sourceFileName

                  .indexOf( '.' ))

                  + "_jtidy.html" ;

           System. out .println( "###########cleanedFileName:" + cleanedFileName);

           FileOutputStream fos = new FileOutputStream(cleanedFileName);

           tidy.parse(fis, fos);

           fis.close();

           fos.close();

       } catch (FileNotFoundException e) {

           log .error(e.getMessage(), e);

       } catch (IOException e) {

           log .error(e.getMessage(), e);

       }

       return new File(cleanedFileName);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值