通过将文件名附加到基础 URL 从服务器目录读取文件
Reading a file from server's directory by appending filename to base URL
我写了一个从服务器读取文件的方法,它工作正常,但现在我修改了它以将文件名作为参数并从服务器读取该文件。这是一个布尔方法,如果在服务器目录中找不到该文件,则 returns 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: No line found"
虽然我服务器上的文件可以访问并且里面有数据。
现在工作正常。问题是循环正在读取我在条件下从扫描仪 class 调用 hasNextLine() 方法的数据,它工作得很好。
我写了一个从服务器读取文件的方法,它工作正常,但现在我修改了它以将文件名作为参数并从服务器读取该文件。这是一个布尔方法,如果在服务器目录中找不到该文件,则 returns 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: No line found" 虽然我服务器上的文件可以访问并且里面有数据。
现在工作正常。问题是循环正在读取我在条件下从扫描仪 class 调用 hasNextLine() 方法的数据,它工作得很好。