java.io.FileNotFoundException:从服务器中的文件夹中检索一组文件

时间:2014-09-11 09:20:05

标签: java client download

我正在尝试使用getfile()从服务器获取一组文件,我得到了这个例外:

  

java.io.FileNotFoundException:E:\ SERVER \ Server Content \ Apps \ icons(访问被拒绝)

这是我的代码:

DownloadController downloadcontroller = null;
try {
    downloadcontroller = ServerConnector.getServerConnector().getDownloadController();
} catch (NotBoundException ex) {
    Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
    Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
} catch (RemoteException ex) {
    Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}

RemoteInputStream file = null;
try {
    file = downloadcontroller.getFile("E:\\SERVER\\Server Content\\Apps\\icons");
} catch (IOException ex) {}

public RemoteInputStream getFile(String fileName) throws IOException {
    // create a RemoteStreamServer (note the finally block which only releases
    // the RMI resources if the method fails before returning.)

    //read data
    RemoteInputStreamServer istream = null;
    try {
        File file = new File(fileName);
        System.out.println(file.exists());
        FileInputStream fileInputStream = new FileInputStream(file);

        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                fileInputStream);
        istream = new SimpleRemoteInputStream(bufferedInputStream);
        // export the final stream for returning to the client

        //send data
        RemoteInputStream result = istream.export();
        // after all the hard work, discard the local reference (we are passing
        // responsibility to the client)
        istream = null;
        return result;
    } finally {
        // we will only close the stream here if the server fails before
        // returning an exported stream
        if (istream != null) {
            istream.close();
        }
    }
}

Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);

1 个答案:

答案 0 :(得分:0)

您无法打开和读取目​​录,使用isFile()和isDirectory()方法来区分文件和文件夹。 您可以使用list()和listFiles()方法获取文件夹的内容(分别用于文件名和文件),您还可以指定选择列出的文件子集的过滤器。