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);
}