这些方法都是读文件的文件的方式
//以下这种读取file的方式都不行
//方法一
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//方法二
File file = new ClassPathResource("generator").getFile();
//方法三
File file = new File(System.getProperty("user.dir"));
//等等
jar中的文件不能以File的形式读取,只能以流的形式读取,如果以file形式读取会报文件找不到 :
java.io.FileNotFoundException:/file:/usr/share/muiuo/mudey-svstem.iar!/BOOT-INF/lib/
或
java.io.FileNotFoundException: class path resource [template ]cannot be resolved to absolute file because it does not reside in the file svstem: jar:file:/usr/share/mp/dev-svstem.iar!/BOOT-INF/Lib/asdf-core-2.2.0.BETA.iar!/template
/**
* 通过流的形式读取文件是没问题的
*/
@Slf4j
@Configuration
public class FreemarkerConfig {
private static freemarker.template.Configuration cfg;
@Autowired
private ResourceLoader resourceLoader;
@Bean
public freemarker.template.Configuration config (){
try {
//template:resource目录下的ftl文件放置目录
SpringTemplateLoader templateLoader = new SpringTemplateLoader(resourceLoader, "classpath:template");
cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
cfg.setTemplateLoader(templateLoader);
cfg.setDefaultEncoding("UTF-8");
} catch (Exception e) {
log.error("error:", e);
}
return cfg;
}
public freemarker.template.Configuration getCfg(){
return cfg;
}
}