[英]Reading a file from server's directory by appending filename to base URL
我已經編寫了一種從服務器讀取文件的方法,並且工作正常,但是現在我已對其進行了修改,以將文件名作為參數並從服務器讀取該文件。 這是一個布爾方法,如果在服務器目錄上找不到該文件,則返回false。 問題是,它捕獲了一個異常,我不知道可能是什么原因,因為服務器目錄中存在該文件,因此不勝感激。
public final boolean readDataFromServer (String fileName)
{
try
{
String url = "http://example.com/FolderName/".concat(fileName);
URL webServer = new URL (url);
Scanner reader = new Scanner(webServer.openStream());
// reading data in variables by using nextLine() method
reader.close();
return true;
}
catch (MalformedURLException e)
{
return false;
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"The file was not found on the server");
return false;
}
catch (Exception e) // this is the block where I'm getting an exception
{
System.out.println("Exception here");
return false;
}
}
編輯:我試圖打印異常,我得到了:“ java.util.NoSuchElementException:找不到行”,而服務器上存在的文件是可訪問的並且包含數據。
現在工作正常。 問題是循環正在條件下從掃描程序類讀取我稱為hasNextLine()方法的數據,並且它運行良好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.