JavaWeb遗漏的知识点

1.

String javax.servlet.ServletContext.getRealPath(String path)方法
官方文档解释:Gets the real path corresponding to the given virtual path.
给定一个虚拟路径(相对路径)返回一个真实路径(绝对路径)
 
在web项目部署后我们要把上传的文件保存到webapp中才可以访问到,这时就需要用这一个方法获取绝对路径
目的是将文件保存到webapp的文件夹中
1 String dir = super.getServletContext().getRealPath("/upload");//获取绝对路径
2                     System.out.println(dir);//结果为E:AlibabaGoupload-download-i18nwebappupload
3                     item.write(new File(dir, fileName));//写入的地址 + 文件名称  把二进制数据写入哪一个文件中

 
 
原文地址:https://www.cnblogs.com/AmosWong/p/9670870.html