博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2获得需要的文件或者访问路径
阅读量:7113 次
发布时间:2019-06-28

本文共 884 字,大约阅读时间需要 2 分钟。

在struts2中,上传文件的时候遇到一个很好用但是失效的方法,找到如下替代。并且测试了一下request可以得到的相关路径。

得到request对象:HttpServletRequest request = ServletActionContext.getRequest() 得到当前项目在服务器的绝对路径(方法失效,但是可用):String path1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";//   D:\javabao\apache-tomcat-7.0.34\webapps\billingsystem\upload 得到当前项目在服务器的绝对路径(推荐使用):String path2 = request.getSession().getServletContext().getRealPath("/upload/");//   D:\javabao\apache-tomcat-7.0.34\webapps\billingsystem\upload 得到根url: String path1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"; //  http://127.0.0.1:8080/ 得到项目发布的名字:System.out.println(ServletActionContext.getRequest().getContextPath());//   /mly 得到当前servlet路径;System.out.println(ServletActionContext.getRequest().getServletPath()); //  /hello_uploadTitle.action

 

转载于:https://www.cnblogs.com/moly/p/6831816.html

你可能感兴趣的文章
U-Boot Makefile文件分析
查看>>
Puppetmaster高可用和可扩展的方案设计
查看>>
[2013EJDE]Osgood type regularity criterion for the $3$D Newton-Boussinesq equations
查看>>
黄疸案
查看>>
[转载]ASP.NET伪静态页面的实现和伪静态在IIS7.0中的配置
查看>>
【转】Android源代码编译命令m/mm/mmm/make分析--不错
查看>>
【SQL】SQL中笛卡尔积、内连接、外连接的数据演示
查看>>
HTTP解析
查看>>
MemCache超详细解读
查看>>
python numpy 教程
查看>>
手机web如何实现多平台分享
查看>>
策略模式和观察者模式
查看>>
详解CALayer 和 UIView的区别和联系
查看>>
eclipse中报错:java.lang.OutOfMemoryError: Java heap space
查看>>
Ubuntu 16.04 grub rescue 模式下修复 grub
查看>>
【Spring】24、<load-on-startup>0</load-on-startup>配置
查看>>
L0 Regularization
查看>>
使用JDBC向Kudu表插入中文数据乱码(转载)
查看>>
spf13-vim安装与使用
查看>>
字体颜色代码表
查看>>