最近项目需要读取一些配置文件(excel),对于怎么取到文件路径,我想到了以下两种方法
1)this.getClass().getClassLoader().getResource("")
2)通过spring自带的org.springframework.core.io.Resource类
例子:
class="java" name="code">
package com.pactera.platform.reqm.core.util;
import org.springframework.core.io.Resource;
/*
* 取得外部模板和标识文件路径的类
*
*/
public class FilePathGet {
// 表示文件路径
private Resource defineFile;
// 模板文件路径
private Resource excelFile;
public Resource getDefineFile() {
return defineFile;
}
public void setDefineFile(Resource defineFile) {
this.defineFile = defineFile;
}
public Resource getExcelFile() {
return excelFile;
}
public void setExcelFile(Resource excelFile) {
this.excelFile = excelFile;
}
}
配置文件
<bean id="defineFilePathGet" class="com.pactera.platform.reqm.core.util.FilePathGet">
<property name="defineFile" value="classpath:/com/pactera/platform/reqm/resources/modelDefine"></property>
<property name="excelFile" value="classpath:/com/pactera/platform/reqm/resources/modelExcel"></property>
</bean>
测试例子
// 模板标识文件名编辑
StringBuilder fileName = new StringBuilder();
fileName.append(filePathGet.getDefineFile().getFile().getAbsolutePath());