springboot部署之后无法获取项目目录的问题:

之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行时,项目中文件上传的功能无法正常运行,其中获取到存放文件的目录的绝对路径的值为空,文件无法上传。问题链接

不清楚此网友具体是怎么实现的,通常我们可以通过如下方案解决:

//获取跟目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath()); //如果上传目录为/static/images/upload/,则可以如下获取:
File upload = new File(path.getAbsolutePath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
System.out.println("upload url:"+upload.getAbsolutePath());
//在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
//在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/

另外使用以上代码需要注意,因为以jar包发布时,我们存储的路径是与jar包同级的static目录,因此我们需要在jar包目录的application.properties配置文件中设置静态资源路径,如下所示:

#设置静态资源路径,多个以逗号分隔
spring.resources.static-locations=classpath:static/,file:static/

以jar包发布springboot项目时,默认会先使用jar包跟目录下的application.properties来作为项目配置文件。

具体项目实战:

resttemplate上传文件:

/**
* 处理文件上传
*/
@RequestMapping("/remoteupload")
@ResponseBody
public String douploadRemote(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile) { if (multipartFile.isEmpty()) {
return "file is empty.";
} String originalFilename = multipartFile.getOriginalFilename();
String newFileName = UUIDHelper.uuid().replace("-", "") + originalFilename.substring(originalFilename.lastIndexOf(".") - 1);
File file = null;
try {
File path = new File(ResourceUtils.getURL("classpath:").getPath());
File upload = new File(path.getAbsolutePath(), "static/tmpupload/");
if (!upload.exists()) upload.mkdirs();
String uploadPath = upload + "\\";
file = new File(uploadPath + newFileName);
multipartFile.transferTo(file); // 提交到另一个服务
FileSystemResource remoteFile = new FileSystemResource(file);
// package parameter.
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("file", remoteFile); String remoteaddr = "http://localhost:12345/test/doupload";
String res = restTemplate.postForObject(remoteaddr, multiValueMap, String.class); return res;
} catch (Exception e) {
return "file upload error.";
} finally {
try{
file.delete();
} catch (Exception e) {
// nothing.
}
return "ok";
}
}

可以参见resttemplate文件上传:

springmvc RestTemplate文件上传

Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件的更多相关文章

  1. Java中获取项目根路径和类加载路径的7种方法

    引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...

  2. html 获取项目根路径

    html 获取项目根路径 function getContextPath(){ var pathName = document.location.pathname; //当前文件的绝度路径 var i ...

  3. js获取项目根路径

    //js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...

  4. Spring下获取项目根路径--good

    Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能.比如某些第三方工具支持通过 ${ ...

  5. javascript 获取项目根路径

    /** * http://localhost:8088/projectName */ function getRootPath(){ //获取当前网址,如: http://localhost:8088 ...

  6. java-动态获取项目根路径

    ${ pageContext.request.contextPath } <hr> <a href="${ pageContext.request.contextPath ...

  7. js获取网站项目根路径

    //js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...

  8. 在HTML页面中获取当前项目根路径的方法

    在HTML页面获取项目根路径的方法: function getRootPath(){ var curPageUrl = window.document.location.href; var rootP ...

  9. JavaWeb项目根路径问题

    jsp中获取项目根路径: 方法① 最顶部增加代码: <% String path = request.getContextPath(); String basePath = request.ge ...

随机推荐

  1. uiautomatorviewer 查看元素新思路

    用adb 命令把图片和uix获取出来,再导入uiautomatorviewer adb shell uiautomator dump /data/local/tmp/uidump.uixadb pul ...

  2. HTML常用模板:用DIV实现网站首页、后台管理首页(整理)

    1. 说明 网上下载的模板,用DIV实现页面模块之间的分隔(不是用frameset/frame).可以选择有无header/menu/footer,主体如何等等.删除了几个我认为重复的.重新命名而已. ...

  3. [LeetCode] Contains Duplicate & Contains Duplicate II

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  4. 网络协议-网络分层、TCP/UDP、TCP三次握手和四次挥手

    网络的五层划分是什么? 应用层,常见协议:HTTP.FTP 传输层,常见协议:TCP.UDP 网络层,常见协议:IP 链路层 物理层 TCP 和 UDP 的区别是什么 TCP/UDP 都属于传输层的协 ...

  5. [vt][xen]xenserver初始安装增加第二块硬盘&xen图形界面安装vm&设置xen里vm开机启动

    为XenServer挂载/增加第二块硬盘的方法 注意: xen6.x和xen7.x vm导出导入是不兼容的.这点有点蛋疼 新买机器后安装xenserver(一般都买刻录机+dvd来刻录xen官网的io ...

  6. 【小白的CFD之旅】22 好网格与坏网格

    网格疏密网格形状其他的一些问题小白的总结郑重申明 网格的作用如此重要,以至于小白纠结了很久.小白知道网格划分过程很大程度上受制于计算资源的限制,但小白还是不太明白,如果计算资源非常充足,不用顾忌资源限 ...

  7. elasticsearch 索引延迟 一致性问题等

    https://www.elastic.co/guide/en/elasticsearch/guide/current/near-real-time.html https://www.elastic. ...

  8. VLAN,GRE,VXLAN

    https://www.cnblogs.com/charles1ee/p/6629009.html VLAN介绍 局域网LAN(多个机器连接一个Hub或者Switch),是一个广播域 VLAN:一台S ...

  9. TensorFlow学习路径【转】

    作者:黄璞链接:https://www.zhihu.com/question/41667903/answer/109611087来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. 【C/C++】关于隐式转换·面试题分析

    题目 以下两个程序片段A 和B ,问哪个能进入循环? 片段A: unsigned short i; unsigned ; ; i < index-; i++) { ........ } 片段B: ...