java项目路径的获取--静态化路径工具类

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;


/**
 * 
 * 静态化路径工具类
 */
public class UtilPath {

	/**
	 * 获取到classes目录
	 * @return path
	 */
	public static String getClassPath(){
		String systemName = System.getProperty("os.name");
		
		//判断当前环境,如果是Windows 要截取路径的第一个 '/'
		if(!StringUtils.isBlank(systemName) && systemName.indexOf("Windows") !=-1){
			return UtilPath.class.getResource("/").getFile().toString().substring(1);
		}else{
			return UtilPath.class.getResource("/").getFile().toString();
		}
	}
	
	/**
	 * 获取当前对象的路径
	 * @param object
	 * @return path
	 */
	public static String getObjectPath(Object object){
		return object.getClass().getResource(".").getFile().toString();
	}
	/**
	 * 获取到项目的路径
	 * @return path
	 */
	public static String getProjectPath(){
		return System.getProperty("user.dir");
	}
	/**
	 * 获取 root目录
	 * @return path
	 */
	public static String getRootPath(){
		return getWEB_INF().replace("WEB-INF/", "");
	}
	/**
	 * 获取输出HTML目录
	 * @return
	 */
	public static String getHTMLPath(){
		return getFreePath() + "html/html/";
	}
	/**
	 * 获取输出FTL目录
	 * @return
	 */
	public static String getFTLPath(){
		return getFreePath() + "html/ftl/";
	}
	/**
	 * 获取 web-inf目录
	 * @return path
	 */
	public static String getWEB_INF(){
		return getClassPath().replace("classes/", "");
	}
	/**
	 * 获取模版文件夹路径
	 * @return path
	 */
	public static String getFreePath(){
		return getWEB_INF() + "ftl/";
	}
	/**
	 * 获取一个目录下所有的文件
	 * @param path
	 * @return
	 */
	public static  File[] getFiles(String path){
		  File file = new File(path);
		  File[] files = file.listFiles();
		  return files;
	}
	/**
	 * 获取当前时间 + 中国时区
	 * @return
	 */
	public static String getDate(){
		SimpleDateFormat sformart=new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); 
		String result = sformart.format(new Date());
		result = result.replace("_", "T");
		result += "+08:00";
		return result;
	}
	/**
	 * 不带结尾的XmlSitemap头部
	 * @return
	 */
	public static String getXmlSitemap(){
		StringBuffer sb = new StringBuffer()
		.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + nextLine())
		.append("<?xml-stylesheet type=\"text/xsl\" href=\"sitemap.xsl\"?>"+ nextLine())
		.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"+ nextLine());
		return sb.toString();
	}
	/**
	 * 文本换行
	 * @return
	 */
	public static String nextLine(){
		 String nextLine = System.getProperty("line.separator");
		 return nextLine;
	}
	/**
	 * 获取domain
	 * @param request
	 * @return
	 */
	public static String getDomain(HttpServletRequest request) {
		return  ((String) request.getSession().getAttribute("nowPath")).replaceAll("(www.)|(.com)|(.net)|(http://)", "").trim();
	}
	/**
	 * 获取images 路径
	 * @return
	 */
	public static String getImages(){
		return getRootPath() + "images/" ;
	}
	
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在微服务项目中,获取静态资源 `static` 文件夹中的图片路径,可以使用 Spring Boot 中提供的 `ResourceLoader` 类。以下是示例代码: ```java import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import java.io.IOException; public class ImagePathUtil { private ResourceLoader resourceLoader; public ImagePathUtil(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public String getImagePath(String imageName) throws IOException { Resource resource = resourceLoader.getResource("classpath:/static/" + imageName); return resource.getURI().toString(); } } ``` 在这个示例中,我们创建了一个 `ImagePathUtil` 工具类,它接受一个 `ResourceLoader` 对象作为构造函数的参数,并提供了一个 `getImagePath` 方法,以便获取 `static` 文件夹中的图片资源路径。 在 `getImagePath` 方法中,我们首先使用 `resourceLoader` 对象的 `getResource` 方法来获取指定图片资源的 `Resource` 对象。这个方法的参数是一个 `classpath:/static/` 前缀和图片资源的名称,它会返回一个表示该资源的 `Resource` 对象。 最后,我们使用 `Resource` 对象的 `getURI` 方法来获取图片资源的 URI,然后将其转换为字符串返回。 使用这个工具类,我们可以方便地获取 `static` 文件夹中的任何图片资源的路径。例如,如果我们要获取名为 `example.jpg` 的图片资源的路径,可以这样调用: ```java ImagePathUtil imagePathUtil = new ImagePathUtil(resourceLoader); String imagePath = imagePathUtil.getImagePath("example.jpg"); ``` 其中 `resourceLoader` 对象可以通过 Spring 的依赖注入机制来注入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值