系統找不到指定路徑,網絡編程

一開始我指定的路徑是相對路徑,就是項目下的路徑
如:http://127.0.0.1:8888/blog/web/index.html

這段代碼就是將blog/web/index.html截取出來賦值給htmlPath ,然後服務器去找這個地址時卻找不到,原因: 相對路徑服務器無法找到,需要在前面加上你項目在你計算機上的路徑,也就是文件的絕對路徑

//把inputStream網絡字節輸入流對象,轉換爲字符緩衝輸入流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        //把客戶端請求信息的第一行讀取出來GET /11 Net/web/index. html HTTP/1.1
        String line = bufferedReader.readLine();
        //把讀取的信息進行切割,只要中間部分/11_ Net/web/index. html
        String[] array = line.split(" ");
        String htmlPath = array[1].substring(1);
        //創建一個本地字節輸入流,構造方法中綁定要讀取的html路徑
        FileInputStream fileInputStream = new FileInputStream(htmlPath);

修改後

//把inputStream網絡字節輸入流對象,轉換爲字符緩衝輸入流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        //把客戶端請求信息的第一行讀取出來GET /11 Net/web/index. html HTTP/1.1
        String line = bufferedReader.readLine();
        //把讀取的信息進行切割,只要中間部分/11_ Net/web/index. html
        String[] array = line.split(" ");
        String urlPath = "C:\\Users\\maoxiaohao\\IdeaProjects\\";
        String htmlPath = array[1].substring(1);
        //創建一個本地字節輸入流,構造方法中綁定要讀取的html路徑
        FileInputStream fileInputStream = new FileInputStream(urlPath+htmlPath);
        ```
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章