Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件
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文件上传:
Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件的更多相关文章
- Java中获取项目根路径和类加载路径的7种方法
引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...
- html 获取项目根路径
html 获取项目根路径 function getContextPath(){ var pathName = document.location.pathname; //当前文件的绝度路径 var i ...
- js获取项目根路径
//js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...
- Spring下获取项目根路径--good
Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能.比如某些第三方工具支持通过 ${ ...
- javascript 获取项目根路径
/** * http://localhost:8088/projectName */ function getRootPath(){ //获取当前网址,如: http://localhost:8088 ...
- java-动态获取项目根路径
${ pageContext.request.contextPath } <hr> <a href="${ pageContext.request.contextPath ...
- js获取网站项目根路径
//js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost ...
- 在HTML页面中获取当前项目根路径的方法
在HTML页面获取项目根路径的方法: function getRootPath(){ var curPageUrl = window.document.location.href; var rootP ...
- JavaWeb项目根路径问题
jsp中获取项目根路径: 方法① 最顶部增加代码: <% String path = request.getContextPath(); String basePath = request.ge ...
随机推荐
- wifiphisher 钓鱼工具的使用
wifiphisher 钓鱼工具的使用一.简介 Wifiphisher是一个安全工具,具有安装快速.自动化搭建的优点,利用它搭建起来的网络钓鱼攻击WiFi可以轻松获得密码和其他凭证.与其它(网络钓鱼) ...
- ( ̄▽ ̄)" 关于河北ETC记账卡的默认密码
去营业厅问了下,是6个1(111111),一般人我不告诉他 ...( _ _)ノ| 好吧,在技术博客里发这种东西合适吗? 还有就是,如果需要打印单位抬头发票的话,需要携带委托书盖单位公章(委托书可以去 ...
- 不错位的java .class 反编译工具推荐
我们经常会反编译看一些class文件,但是反编译出来的文件里面会有很多杂乱的东西 一直以来都是用的idea来反编译的,只要把class文件往里面一拖就行了 这么用没问题,用来看看源码什么的都OK 但是 ...
- 解决 emoji表情存入数据库为' ??? '
今天把mysql切换为mariaDB,这是题外话 主要是emoji标签存入数据库会发现出现三个???的问题 页面上也显示不出来,解决办法只要把内容从数据库里拿出来的时候解码即可 for (Feedba ...
- VS2015 调试中断点突然失效的解决办法、VS调试时关闭调试让浏览器继续保留页面
VS2010 调试中断点突然失效的解决办法 问题描述:在调试前加了断点,但debug时红色的断点变成透明的圆圈加一个感叹号,执行到该处时也不会停止. 这个问题遇到过几次了,前几次都没怎么注意,有时候是 ...
- A implementaion for 2D blue noise
http://www.redblobgames.com/articles/noise/2d/
- Spark(Hive) SQL中UDF的使用(Python)【转】
相对于使用MapReduce或者Spark Application的方式进行数据分析,使用Hive SQL或Spark SQL能为我们省去不少的代码工作量,而Hive SQL或Spark SQL本身内 ...
- package.json中配置浏览器
"browserlist":[ ">=1%", "last 2 versions" ],
- XnView查看图片实际大小
找了很多软件,只有它可以显示尺寸大小. 各位如有别的发现,可留言推荐,谢谢
- AIX上打包排除某些文件/文件夹
Syntax X/Open Standards: tar {-c|-r|-t|-u|-x} [-B] [ -d ] [ -E ] [ -F ] [-h ] [ -i ] [ -l ] [ -m ] [ ...