【Java开发记录】程序打成jar相关静态资源加载情况(五)

admin 2021年9月28日08:00:00评论78 views字数 3261阅读10分52秒阅读模式

【Java开发记录】

程序打成jar相关静态资源加载情况(五)


日常程序编写打包会出先一个就是静态资源打包到jar里面加载不了相关情况、项目加载文件路径情况:

主要是还是new File()进行文件的加载和读取:

相对路径 绝对路径加载内容:

大概jar示意图:

程序class路径:

com.thelostworld.core.XXXXX_FileUpload

静态资源路径根目录下面:

cXDQEmI1.zip


一、this.getClass().getResource("")方式进行path加载

这个时候jar运行加载路径:

就是jar目录的根目录文件:

URL fileURL=this.getClass().getResource("cXDQEmI1.zip");/Users/thelostworld/Documents/idea_program/thelostworld/target/thelostworld.jar!/cXDQEmI1.zip

读取到jar程序包的根路径:

通过这个方式指定路径到jar包中的静态文件


二、System.getProperty("user.dir")+""方式进行path加载

这个是定位程序当前运行的path+这个方式建议加载外部的资源方便

/Users/thelostworld/Documents/idea_program/thelostworld/target/cXDQEmI1.zip

【Java开发记录】程序打成jar相关静态资源加载情况(五)

不同的环境运行的地区系统当前的path:

/Users/thelostworld/Desktop/cXDQEmI1.zip

三、通过项目绝对路径去进行文件加载(不建议这个,灵活性不好)-自己debug测试方便,打包后不能使用

项目绝对路径

new file("/Users/thelostworld/Documents/idea_program/thelostworld/src/main/resources/cXDQEmI1.zip")

项目相对

new file("src/main/resources/cXDQEmI1.zip")

类似上面的直接的项目路径(替换一个位置就不行了)

【Java开发记录】程序打成jar相关静态资源加载情况(五)

也可通过当前类加载这路径:

CLASSPATH="/cXDQEmI1.zip" //当前类的绝对路径class.getResource( "/").getFile()输出结果:/Users/thelostworld/Documents/idea_program/thelostworld///指定CLASSPATH文件的绝对路径class.getResource(CLASSPATH).getFile()输出结果:/Users/thelostworld/Documents/idea_program/thelostworld/target/cXDQEmI1.zip

总结:目前测试好几种方式个人建议加载外部资源,资源可替换和内容方便修改


补充扩展

同时也查询一些其他的java获取的资源:

Java在WEB项目中获取文件路径
jsp中获得文件路径servlet中获得文件路径java中获得文件路径jsp中获得文件路径1、根目录所对应的绝对路径:request.getRequestURI();2、文件的绝对路径:application.getRealPath(request.getRequestURI())3、当前web应用的绝对路径:application.getRealPath("/")4、取得请求文件的上层目录:
newFile(application.getRealPath(request.getRequestURI())).getParent()servlet中获得文件路径1、根目录所对应的绝对路径:request.getServletPath()2、文件的绝对路径:request.getSession().getServletContext().getRealPath(request.getRequestURI())3、当前web应用的绝对路径:servletConfig.getServletContext().getRealPath("/")
注:ServletContext对象获得几种方式:javax.servlet.http.HttpSession.getServletContext()javax.servlet.jsp.PageContext.getServletContext()javax.servlet.ServletConfig.getServletContext()java中获得文件路径1、Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()2、MyClass.class.getClassLoader().getResource("").toURI().getPath()3、ClassLoader.getSystemResource("").toURI().getPath()4、MyClass.class.getResource("").toURI().getPath()5、MyClass.class.getResource("/").toURI().getPath()6、newFile("/").getAbsolutePath().toURI().getPath()7、System.getProperty("user.dir").toURI().getPath()


参考:

https://blog.csdn.net/weixin_33733810/article/details/91882026?utm_term=jar%E5%8C%85%E4%B8%AD%E6%89%BE%E4%B8%8D%E5%88%B0%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-2-91882026&spm=3001.4430

https://zhidao.baidu.com/question/2117062613484208307.html


⚠️

()!




thelostworld

https://www.zhihu.com/people/fu-wei-43-69/columnshttps://www.jianshu.com/u/bf0e38a8d400CSDNhttps://blog.csdn.net/qq_37602797/category_10169006.htmlhttps://www.cnblogs.com/thelostworld/FREEBUFhttps://www.freebuf.com/author/thelostworld?type=articlehttps://www.yuque.com/thelostworld


相关推荐: FRP内网穿透

简介FRP 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。下载地址:https://github.com/fatedier/frp/…

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年9月28日08:00:00
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【Java开发记录】程序打成jar相关静态资源加载情况(五)https://cn-sec.com/archives/552378.html

发表评论

匿名网友 填写信息