获取web项目中某个文件的绝对路径——使用ServletContext的getRealPath使创建的文件位于部署目录下

package lzh;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet( "/Path")
public class PathServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        String realPath = servletContext.getRealPath("hello.txt");
        File file1=new File(realPath);
        boolean newFile1 = file1.createNewFile();
        System.out.println(newFile1);
    }
}

 

你可能感兴趣的:(获取web项目中某个文件的绝对路径——使用ServletContext的getRealPath使创建的文件位于部署目录下)