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. Fast Algorithm To Find Unique Items in JavaScript Array

    When I had the requirement to remove duplicate items from a very large array, I found out that the c ...

  2. MySQL的DDL语句、DML语句与DCL语句

    背景:近几年,开源数据库逐渐流行起来.由于具有免费使用.配置简单.稳定性好.性能优良等优点,开源数据库在中低端应用上占据了很大的市场份额,而 MySQL 正是开源数据库中的杰出代表.MySQL 数据库 ...

  3. elk 使用中遇到的问题(kafka 重复消费)

    问题描述: 在使用过程中,当遇到大量报错的时候,我们到eagle后台看到报错的那个consumer的消费情况到到lag 远远大于0(正常情况应该为0),activie  节点没有,kibana面板上没 ...

  4. influxdb 配置文件注释

    ### Welcome to the InfluxDB configuration file. # The values in this file override the default value ...

  5. 对 /sbin/nologin 的理解

    对 /sbin/nologin 的理解 系统账号的shell使用 /sbin/nologin ,此时无法登陆系统,即使给了密码也不行.   所谓“无法登陆”指的仅是这个用户无法使用bash或其他she ...

  6. java线程五种状态

    java线程五种状态: 创建 -> 就绪 -> 运行 -> 销毁 创建 -> 就绪 -> 运行 -> 等待(缺少资源) -> 销毁 下图:各种状态转换

  7. 如何使你的Android应用记住曾经使用过的账户信息

    原文:http://android.eoe.cn/topic/android_sdk 当您记住他们的名字时,每个人都会很喜欢.最简单的一个例子,您能够做的,让您的应用更加受人喜爱的,最有效的方法是记住 ...

  8. 牛腩学用MUI做手机APP

    斗鱼直播间直播学习撸码,最终目标是用MUI做一个手机APP(暂定android平台,攒钱买IPHONE 7SE!!!),直播内容含整个软件APP的制作过程(含后台接口的制作,放到自己买的阿里云服务器, ...

  9. 每日英语:The Most Destructive, Unpredictable Force in Tech

    What's the most destructive force in the tech world, the thing that has nearly killed BlackBerry, pu ...

  10. Zookeeper之Zookeeper的Client的分析

    1)几个重要概念 ZooKeeper:客户端入口 Watcher:客户端注册的callback ZooKeeper.SendThread: IO线程 ZooKeeper.EventThread: 事件 ...