讀取nginx文件

//讀取ngnix文件(網絡地址)

public static void  readFile(){
		int HttpResult; // 服務器返回的狀態
	    try {
			String filePath = "http://192.168.1.25/File/file/20180913/netmethod.txt";
	    	URL url =new URL(filePath); // 創建URL
            URLConnection urlconn = url.openConnection(); // 試圖連接並取得返回狀態碼
            urlconn.connect();
            HttpURLConnection httpconn =(HttpURLConnection)urlconn;
            HttpResult = httpconn.getResponseCode();
            if(HttpResult != HttpURLConnection.HTTP_OK) {//(HTTPResult爲200的時候說明地址是訪問到了)
                System.out.print("無法連接到");
            }else{
            	/*讀取.txt文件*/
            	InputStream inputStream  = urlconn.getInputStream();
            	InputStreamReader reader = new InputStreamReader(inputStream,"utf-8");
            	BufferedReader br = new BufferedReader(reader);
            	StringBuilder builder = new StringBuilder();
            	String str = null;
            	while ((str = br.readLine())!=null) {
            		builder.append(str);
            		builder.append(System.getProperty("line.separator"));
				}
            	System.out.println(builder.toString());
            	/*讀取Excel
            	 * int filesize = urlconn.getContentLength(); // 取數據長度
            	XSSFWorkbook xssfWorkbook = new XSSFWorkbook(urlconn.getInputStream());
            	int numberOfSheets = xssfWorkbook.getNumberOfSheets();
            	System.out.println(numberOfSheets);*/
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章