打包成jar后的文件读取问题

打包成JAR后的配置文件读取方式和没打包的不一样,
为了兼容两种模式,
1、spring配置文件中引用属性文件方法
bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="locations">
<list>
<value>file:**/config/pos_jdbc.properties</value>
</list>
</property>
</bean>

用file:**让spring去查找文件

2、手工读取spring配置文件的方法,如SpringUtils中

private static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:config/applicationContext_Pos.xml");


3、读取文件时应该区分文件在jar中还是jar的同级目录中,如图片等,一般是不打包到jar中,这么读取文件

/**
* 获取项目的文件的输入流,如果项目在jar中,则先在jar中查找文件,找不到再到jar外面查找文件
* @param filePath
* @return
* @throws IOException
*/
public static InputStream getProjectFile(String filePath) throws IOException
{
InputStream in = null;

String currentJarPath = URLDecoder.decode(Constants.class
.getProtectionDomain().getCodeSource().getLocation().getFile(),
"UTF-8"); // 获取当前Jar文件名
String path = Constants.class.getResource("").getPath();
if (path.indexOf("!") != -1)
{// 在jar中
logger.info(filePath + " is in jar");
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry(filePath);
if (dbEntry != null)
in = currentJar.getInputStream(dbEntry);
}
if (in == null)
{
// String loaderPath =
// Thread.currentThread().getContextClassLoader().getResource("").getPath();
logger.info(filePath + " is not in jar");
String loaderPath = currentJarPath;
String packPath = Constants.class.getPackage().toString();
packPath = packPath.replace("package ", "");
packPath = "/" + packPath.replace(".", "/");
if (!loaderPath.endsWith("/"))
loaderPath = loaderPath.substring(0, loaderPath
.lastIndexOf("/"))
+ "/";
loaderPath = loaderPath.replace(packPath, "");
String tPath = loaderPath + filePath;
logger.info("file the path =" + tPath);
in = new FileInputStream(tPath);
}
return in;
}

4、读取图片的方法
jar中时使用相对路径读取,路径不能以/开头,且不能用getResource,方法如下

//imgPath在jar中是空字符串,非jar中是/
public static ImageIcon createImageIcon(String path) {
String tmp = path;
if(tmp != null && tmp.startsWith("/"))
{
path = path.substring(1);
}
try
{
String imgPath = Constants.getImgPath();
if("".equals(imgPath))
{//在jar中
return new ImageIcon(path);
}
else
{
return new ImageIcon(ImageIcon.class.getResource(imgPath + path));
}
}
catch(Exception e)
{
LogUtils.Log(logger, e);
return null;
}
}


5、保存文件时,使用绝对路径,绝对路径获取方式

private static String RealClzPath=null; //classes目录的物理路径
private static Logger logger = Logger.getLogger(Constants.class);
private static String imgPath = null; //图片路径前置,如果项目打包成JAR,前置为空,如果项目没打包,这前置为"/"
static
{
String currentJarPath = null;
try
{
currentJarPath = URLDecoder.decode(Constants.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
LogUtils.Log(logger, e);
}
String path = Constants.class.getResource("").getPath();
int pos = path.indexOf("!");
if(pos != -1)
{//在jar中
logger.info("class is in jar");
RealClzPath = path.substring(0,pos);
pos = RealClzPath.lastIndexOf("/");
imgPath = "";
if(pos != -1)
{
RealClzPath = path.substring(0,pos+1);
}
}
else
{
//String loaderPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
imgPath = "/";
String loaderPath = currentJarPath;
String packPath = Constants.class.getPackage().toString();
packPath = packPath.replace("package ", "");
packPath = "/" + packPath.replace(".", "/");
loaderPath = loaderPath.replace(packPath, "");
RealClzPath = loaderPath;
}
logger.info("RealClzPath = " + RealClzPath + " imgPath=" + imgPath);

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值