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 ...
随机推荐
- python selenium 使用unittest 示例
python selenium 使用unittest 示例 并等待某个元素示例 from selenium.webdriver.support.ui import WebDriverWait from ...
- apiDoc自动生成api文档
在自定生成api文档方面以前都是使用swagger.json结合swagger工具来生成文档,偶然发现了apidoc这个生成api的工具,发现使用起来比swagger更加简单,下面整理一下使用过程: ...
- Windows 7 添加SSD硬盘后重启卡住正在启动
楼主办公电脑,原来只配置了一块机械硬盘,用着总很不顺心,于是说服领导给加了块SSD固态硬盘. 操作如下: 1.在PE下分区格式化新固态硬盘,将原来机械硬盘的C盘GHOST备份后还原到新固态硬盘: 2. ...
- php分享二十三:字符编码
1:ASCII 在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写).以及0.1等数字还有一些常用的 ...
- untiy 2d游戏平面直角坐标系的旋转应用
2d旋转的应用 1 :条件1 (已知) 创建一个平面直角坐标系 左上角为(0,0),能够把一个加入了UIPanel组件的物体(名字叫Father)移至UIRoot左上角 Y和Z轴都旋转180度.这样你 ...
- 运行jar乱码问题
请使用 1.java -jar -Dfile.encoding=utf-8 dapao.jar 2.请使用URLDecode,URLEncode 3.请使用unicode编码格式 bat运行当前目录 ...
- 深入理解Linux内核-块设备驱动程序
扇区: 1.硬盘控制器将磁盘看成一大组扇区2.扇区就是一组相邻字节3.扇区按照惯例大小设置位512字节4.存放在块设备中的数据是通过它们在磁盘上的位置来标识,即首个扇区的下标和扇区的数目.5.扇区是硬 ...
- mydqldump 备份数单库 然后还原数据的时候报:ERROR 1881 (HY000) at line 52: Operation not allowed when innodb_forced_recovery > 0.
修改my.cnf innodb_force_recovery = 1 修改为: innodb_force_recovery = 0
- openfire维持在线状态,监听消息
public static void testLoginStatus()throws XMPPException,InterruptedException { AccountManager accou ...
- c与c++相互调用机制分析与实现
c++通常被称为Better c,多数是因为c++程序可以很简单的调用c函数,语法上基本实现兼容.最常用的调用方式就是c++模块调用c实现的dll导出函数,很简单的用法,使用extern " ...