1,文件的硬盘存储路径(文件存放在服务器的根目录,而不是项目,防止服务器重启,资源丢失)
得到项目的名字:如,renli
String path1 = request.getContextPath();
path1=path1.substring(1);
得到项目的绝对路径:如,F:apache-tomcat-8.0.33webapps enliresources
String path2 = request.getSession().getServletContext().getRealPath("");
得到服务器的根目录:如,F:apache-tomcat-8.0.33webapps
String path2 = request.getSession().getServletContext().getRealPath("").replace(path1, "");
path2=path2.substring(0, path2.length()-1)+"resources";
2,资源的网络访问路径:http://localhost:8080/resources
String url = "http://" + request.getServerName()+ ":" + request.getServerPort()+"/resources";
3,截取文件名:
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
UUID uuid = UUID.randomUUID();
fileName = uuid + "." + prefix;
4,判断file为空
file.isEmpty()