Chinaunix首页 | 论坛 | 博客
  • 博客访问: 977537
  • 博文数量: 152
  • 博客积分: 4937
  • 博客等级: 上校
  • 技术积分: 1662
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-05 16:55
文章分类

全部博文(152)

文章存档

2013年(12)

2012年(6)

2011年(58)

2010年(43)

2009年(1)

2008年(15)

2007年(17)

我的朋友

分类: WINDOWS

2011-07-01 14:33:17

 
URLUtil uu = new URLUtil();
    String FILE_PATH =uu.getClassPath(Attach.class);
    FILE_PATH =uu.getClassPath(Attach.class);
    if(FILE_PATH.indexOf("WEB-INF\\classes")>-1){
    
     this.path=  FILE_PATH.substring(0,FILE_PATH.indexOf("WEB-INF\\classes")+7)+"";
    
    }else{
    
     this.path = "upload";
    }
 
package com.bean;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
 /**
 *


 * Title:URL辅助工具类
  *


  *
 
  *


  *
  */
  public class URLUtil {
     /**
      *
      * Description:取得当前类所在的文件
      *
    
      */
         public static File getClassFile(Class clazz) {
         URL path = clazz.getResource(clazz.getName().substring(
                 clazz.getName().lastIndexOf(".") + 1)
                 + ".class");
         if (path == null) {
             String name = clazz.getName().replaceAll("[.]", "/");
             path = clazz.getResource("/" + name + ".class");
         }
         return new File(path.getFile());
     }
    /**
      *
      * Description:同getClassFile 解决中文编码问题
      *
      * @param clazz
   
      * @since:Sep 21, 2008 1:10:12 PM
      */
     
      public static String getClassFilePath(Class clazz) {
         try {
             return java.net.URLDecoder.decode(getClassFile(clazz)
                     .getAbsolutePath(), "UTF-8");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
             return "";
         }
     }
    /**
      *
      * Description:取得当前类所在的ClassPath目录
      *
      * @param clazz
  
      * @since:Sep 21, 2008 12:32:27 PM
      */
      public static File getClassPathFile(Class clazz) {
         File file = getClassFile(clazz);
         for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
             file = file.getParentFile();
         if (file.getName().toUpperCase().endsWith(".JAR!")) {
             file = file.getParentFile();
         }
         return file;
     }
     /**
      *
      * Description: 同getClassPathFile 解决中文编码问题
      *
      * @param clazz
 
      * @since:Sep 21, 2008 1:10:37 PM
      */
     
     
      public static String getClassPath(Class clazz) {
         try {
             return java.net.URLDecoder.decode(getClassPathFile(clazz)
                     .getAbsolutePath(), "UTF-8");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
             return "";
         }
     }
    public static void main(String[] args) throws UnsupportedEncodingException {
         System.out.println(getClassFilePath(URLUtil.class));
         System.out.println(getClassPath(URLUtil.class));
     }
 }

 
阅读(621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~