경로 문제 정리

7169 단어 서버

경로 문제 정리

  • 경로 문제 정리
  • 의 차이
  • 1 서버 및 클라이언트 브라우저
  • 2 응용 실례
  • 서버 측 상대 경로 읽기 파일
  • 1tomcatbin 디렉터리에 위치
  • 2 웹 응용 프로그램에서 자원 파일을 불러오는 방법
  • 운영 체제 차이점
  • 1 공통점
  • 디렉터리 구분자 차이
  • URIURL


  • 내 홈페이지 www.csxiaoyao.com

    1 "/" 의 차이점


    1.1 서버 및 클라이언트 브라우저


    서버/웹 루트의 루트 디렉터리 (항목 이름 필요 없음) 브라우저/웹 앱의 루트 디렉터리 (항목 이름 쓰기 필요)

    1.2 응용 사례


    전달
    request.getRequestDispatcher("/target.html").forward(request, response);

    리디렉션 요청
    response.sendRedirect("/project/target.html");

    html 페이지 초연결
    response.getWriter().write("<html><body><a href='/project/target.html'> a>body>html>");

    html 페이지의form 제출 주소
    <form action='/project/target.html'>

    2 서버 측 상대 경로에서 파일 읽기


    2.1 “.” tomcat/bin 디렉터리로 이동

    // 
    File("./src/db.properties");

    2.2 웹 응용 프로그램에서 자원 파일을 불러오는 방법


    메서드1: getRealPath() 읽기, 리소스 파일의 절대 경로 반환
    String path = this.getServletContext().getRealPath("/WEB-INF/db.properties");
    File file = new File(path);
    FileInputStream in = new FileInputStream(file);

    메서드 2: getResourceAsStream () 에서 리소스 파일을 가져와 입력 흐름을 반환합니다.
    InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
    // 
    Properties prop = new Properties();
    prop.load(in);
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");

    3 운영 체제 차이점


    3.1 공통점


    절대 경로: 이 파일의 전체 경로는 일반적으로 디스크 문자로 시작됩니다.상대 경로: 상대 경로는 자원 파일이 현재 프로그램이 있는 경로를 기준으로 합니다...현재 경로...이전 레벨 경로

    3.2 디렉터리 구분자 차이


    윈도우즈 기기의 디렉터리 구분자는\이고, linux 기기의 디렉터리 구분자는/이며, 윈도우즈에서\와/를 디렉터리 구분자로 사용할 수 있으며,/를 쓸 때 하나만 쓰면 경로 예시가 됩니다.
    ----linux----
    /home/sunshine/data.txt
    ./     
    ../    
    ----windows----
     "\" "\\"
    E:\sunshine\a.txt
    E:/sunshine/a.txt
    E:
    ../../
    System.out.println(" :"+ File.separator);
    System.out.println(" :"+ file4.getAbsolutePath());//D:\Workspaces\java_test\.
    File parentFile = new File("E:\\");
    File file1 = new File("E:"+File.separator+"a.txt");
    File file2 = new File("E:/a.txt"); 
    File file3 = new File("E:\\","a.txt");
    File file4 = new  File(".");
    File file5 = new File("..\\..\\a.txt");

    4 URI、URL


    URI:Uniform Resource Identifier, Uniform Resource 식별자 URL:Uniform Resource Locator, Uniform Resource 포지셔닝 문자
    URL은 URI의 특례입니다. URL은 유일하게 자원을 포지셔닝할 수 있습니다[URI 예시]mailto:[email protected][URL 예제]file:///E:/sunshine/a.txt http://www.csxiaoyao.com/ ftp://home.ustc.edu.cn

    좋은 웹페이지 즐겨찾기