gpt4 book ai didi

java - 通过将文件名附加到基本 URL 来从服务器目录读取文件

转载 作者:行者123 更新时间:2023-12-01 10:56:29 27 4
gpt4 key购买 nike

我编写了一个从服务器读取文件的方法,它工作正常,但现在我已经修改它以将文件名作为参数并从服务器读取该文件。这是一个 boolean 方法,如果在服务器目录中找不到该文件,则返回 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() 方法的数据,并且它工作得很好。

关于java - 通过将文件名附加到基本 URL 来从服务器目录读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com