was 图片路径与存储 与tomcte不同

来源:互联网 发布:罗盘软件 编辑:程序博客网 时间:2024/05/16 05:56


    public static File getClassFile(Class clazz){
        URL path = clazz.getResource(clazz.getName().substring(
                clazz.getName().lastIndexOf(".")+1)+".classs");
        if(path == null){
            String name = clazz.getName().replaceAll("[.]", "/");
            path = clazz.getResource("/"+name+".class");
        }
        return new File(path.getFile());
    }
    /**
     * 得到当前类的路径
     * @param clazz
     * @return
     */
    public static String getClassFilePath(Class clazz){
        try{
            return java.net.URLDecoder.decode(getClassFile(clazz).getAbsolutePath(),"UTF-8");
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return "";
        }
    }
   
    /**
     * 取得当前类所在的ClassPath目录,比如tomcat下的classes路径
     * @param clazz
     * @return
     */
    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;
    }
    /**
     * 取得当前类所在的ClassPath路径
     * @param clazz
     * @return
     */
    public static String getClassPath(Class clazz){
        try{
            return java.net.URLDecoder.decode(getClassPathFile(clazz).getAbsolutePath(),"UTF-8");
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return "";
        }
    }

下附我写的代码调用类


String classPath=getClassPath(this.getClass());
  //System.out.println(classPath);
  String rooPath = classPath.replaceAll("WEB-INF/classes/","");
  int j=rooPath.indexOf("WEB-INF");
  String rootPath=rooPath.substring(0,j);
  System.out.println(""+rootPath);
  String filePath = "";
  if("0".equals(vatType)){
   filePath = rootPath+"/template/bill/zzszy_invoice.png";
  }else{
   filePath = rootPath+"/template/bill/zzspy_invoice.png";
  }
  System.out.println("模板路径"+filePath);

fileName = fileName+System.currentTimeMillis() + ".png";
   String nF = rootPath+"/template/bill/outBill/" + fileName;
   System.err.println("输出路径"+nF);

0 0
原创粉丝点击