ServletContext.getRealPath() 的输入参数要以"/"开头

本文详细解析了ServletContext.getRealPath()方法的使用规则,包括输入参数的格式要求及常见错误处理,帮助开发者避免在实际项目中遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

ServletContext.getRealPath() 的输入参数要以"/"开头

  2640人阅读  评论(0)  收藏  举报

ServletContext.getRealPath() 是从当前servlet 在tomcat 中的存放文件夹开始计算起的


比如,有个servlet 叫 UploadServlet,它部署在tomcat 下面以后的绝对路径如下:

"C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServlet"


那么,

ServletContext.getRealPath("/") 返回 "C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServlet"

ServletContext.getRealPath("/attachment") 返回 "C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServlet\attachment"

ServletContext.getRealPath("attachment") 会导致NullPointerException


结论就是:

在使用ServletContext.getRealPath() 时,传入的参数是从 当前servlet 部署在tomcat中的文件夹算起的相对路径,要以"/" 开头,否则会找不到路径,导致NullPointerException


解决问题过程中,发现这位大哥做了类似的记录 ServletContext .getRealPath,但其观点,不敢苟同。自己观察到的结果是:

ServletContext.getRealPath("/") 返回 "C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServlet"

ServletContext.getRealPath("/") + "xxx" 返回 "C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServletxxx"

ServletContext.getRealPath("/") + "/xxx" 返回 "C:\Program Files\apache-tomcat-8.0.3\webapps\UploadServlet\xxx"

即,getRealPath() 返回的字符串结尾不带"/"


注意,Windows下,在Eclipse debug过程中,看到的getRealPath() 返回的字符串其实是长这样子的:

"C:\\Program Files\\apache-tomcat-8.0.3\\webapps\\UploadServlet\\attachment"

"\\"中的第一个"\"是转义符,你懂的。


BTW,刘京华的《Java Web整合开发 之 王者归来》第71页代码中的 getRealPath("attachment") 以及附带光盘中的代码,着实坑爹……


this.fileService.uploadFileToPath("CodeData.js", "js", dataString.getBytes(StandardCharsets.UTF_8), request); public FileDTO uploadFileToPath(String fileName, String path, byte[] bytes, HttpServletRequest request) throws Exception { FileHandleFactory fileHandle = this.getHandleFactory(); return fileHandle.uploadFileToPath(fileName, path, bytes, request); } public FileDTO uploadFileToPath(String fileName, String path, byte[] bytes, HttpServletRequest request) throws Exception { String rootRealPath = this.getRootRealPath(request); return this.uploadFileToPath(fileName, rootRealPath, path, bytes); } private FileDTO uploadFileToPath(String fileName, String rootRealPath, String destPath, byte[] bytes) throws Exception { if (StringUtils.isNotEmpty(destPath)) { if (!destPath.endsWith("/")) { destPath = destPath + "/"; } if (!destPath.startsWith("/")) { destPath = "/" + destPath; } } else { destPath = "/"; } String destRealPath = rootRealPath + destPath; try { File dir = new File(destRealPath); if (!dir.exists()) { dir.mkdirs(); } File file = new File(destRealPath + fileName); FileUtils.writeByteArrayToFile(file, bytes); } catch (Exception var8) { throw new ApplicationException("文件保存失败!"); } String fileExtName = fileName.lastIndexOf(".") > 0 ? fileName.substring(fileName.lastIndexOf(".") + 1) : ""; FileDTO dto = new FileDTO(); dto.setFileId(destPath + fileName); dto.setFileName(fileName); dto.setFileExtName(fileExtName); return dto; } 结合一下,查看一下CodeData.js输出到哪里去了
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值