springboot上传文件使用Request对象中可以获取各种路径信息

场景描述:使用springboot上传文件到服务器,但是需要将文件路径返回给前台。项目中使用的是用虚拟路径配置返回形式如下:

http://172.22.5.12:8083/puhuicrm/photo/appletimg/e8d4b60fd3ff4bcd89d47413201ca337.jpg

测试环境:springboot 2.0.3+jdk1.8+tomcat 8.0

正式环境:springboot 2.0.3+jdk1.8+tomcat 8.0+nginx

先上本地代码:

出现的问题:

1.由于测试使用http协议,但是正式版本使用https协议,所以部署正式版本时就会由于写死http://时就会导致路径错误

解决办法:使用request获取request.getScheme()+"://"

2.更改之后,正式获取的路径https://crm.puh**lth.com:80/photo/product/2019/1/22//beaf97f5-9f94-4cdb-b2aa-9168962b3fe2.pdf。仍旧打不开。

是因为crm.pu****lth.com已经代理端口号,现在获取到的80是nginx默认的端口号。而现在正式使用https的端口号是443.也就是说,443的端口路径就是可以访问的。因此解决办法是修改nginx的端口和https访问一致的443.

修改参考

https://blog.csdn.net/u011922760/article/details/52893438

https://blog.csdn.net/lsm135/article/details/51879453

request获取URL知识点总结:

String path = request.getContextPath();  

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

String remoteAddress=request.getRemoteAddr();  

String servletPath=request.getServletPath();  

String realPath=request.getRealPath("/");  

String remoteUser=request.getRemoteUser();  

String requestURI=request.getRequestURI();  

out.println("path:"+path+"<br>");  

out.println("basePath:"+basePath+"<br>");  

out.println("remoteAddr:"+remoteAddress+"<br>");  

out.println("servletPath:"+servletPath+"<br>");  

out.println("realPath:"+realPath+"<br>");  

out.println("remoteUser:"+remoteUser+"<br>");  

out.println("requestURI:"+requestURI+"<br>");  

结果:  

path:/WebDemo  

basePath:http://localhost:8683/WebDemo/  

remoteAddr:127.0.0.1  

servletPath:/index.jsp  

realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\  

remoteUser:null  

requestURI:/WebDemo/index.jsp  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值