//讀取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();
}
}
}