LocationPathUtil

package com.css.common.util;

import java.net.URL;
import java.util.Properties;

/**
 * 得到服务器的相对路径
 *
 * @version 1.0
 *
 */
public class LocationPathUtil {

 /**
  * 单利模式
  */
 private static LocationPathUtil config = new LocationPathUtil();

 private LocationPathUtil() {
 }

 public static LocationPathUtil getInstance() {
  return config;
 }

 /**
  * 这个方法获得配置文件的路径
  *
  * @param config
  *            配置文件的名称
  * @return 返回配置文件路径字符串,格式为 盘符:\·········\WEB-INF
  */
 public static String getPath(String config) {
  URL url = LocationPathUtil.class.getClassLoader().getResource("");
  Properties props=System.getProperties(); //系统属性
  String os = props.getProperty("os.name");
  String xmlPath = url.getPath().substring(0,url.getPath().lastIndexOf("/"));
  xmlPath = xmlPath.substring(0, xmlPath.lastIndexOf("/"));
  xmlPath += config + "";
  xmlPath = unicode2Char(xmlPath);
  String temp=xmlPath.substring(1);
  if(os.indexOf("Windows") != -1){
   return temp;
  }else if(os.indexOf("Linux") != -1){
   return "/"+temp;
  }
  return null;
 }

 /**
  * <p>
  * 处理路径中中文问题
  * </p>
  * <p>
  * 我们读取出来的路径是一个acsi类型的字符串,通过这个方法将这个路径转换成UTF-8类型的
  * </p>
  *
  * @param s
  *            配置文件字符串
  * @return 返回转换字符编码之后的字符串
  */
 private static String unicode2Char(String s) {
  String _s = null;
  String s1 = null;
  String s2 = null;
  int i = 0, j = 0;
  if (s.indexOf("%20") != -1) {
   s = s.replaceAll("%20", " ");
  }
  try {
   while (s.indexOf("%") != -1) {
    i = s.indexOf("%") + 1;
    j = i + 8;
    s1 = s.substring(0, i - 1);
    s2 = s.substring(j);
    s = s.substring(i, j);
    s = s1+ new String(new byte[] {
      (byte) Integer.parseInt(s.substring(0,2), 16),
      (byte) Integer.parseInt(s.substring(3,5), 16),
      (byte) Integer.parseInt(s.substring(6,8), 16) }, "utf-8") + s2;}
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return s;
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值