【问题标题】:How to browse the file system of a server machine when connecting with a client (java)与客户端连接时如何浏览服务器机器的文件系统(java)
【发布时间】:2011-08-08 15:47:08
【问题描述】:

我正在制作概念证明,以将业务代码与 ps3 媒体服务器 (http://www.ps3mediaserver.org/) 的 gui 分离。为此,我在 source forge (http://sourceforge.net/projects/pms-remote/) 上托管了一个项目。客户端应该是一个简单的前端,可以从有权连接到服务器的网络中的任何位置配置服务器。

在服务器端,所有服务都使用 javax.jws 暴露出来,客户端代理已经使用 wsimport 生成。

当前功能的其中一个功能(实际上是唯一的阻塞功能)是定义将由服务器共享的文件夹。由于客户端和服务器现在作为单个应用程序在同一台机器上运行,因此浏览其文件系统很简单。

问题:我想通过网络服务暴露服务器机器的文件系统。这将允许任何客户端(我目前正在使用的客户端与使用 java swing 的原始客户端相同)显示可用文件夹并选择将由媒体服务器显示的文件夹。最后,我唯一感兴趣的是绝对文件夹路径(字符串)。

我想我会找到一个库来给我这个功能,但找不到。 使用 UNC 路径浏览文件并访问远程计算机似乎不可行,因为它对用户不透明。

目前我不想担心安全问题,等其余的似乎可行后,我会解决这些问题。

如有任何意见,我将不胜感激。 谢谢,菲利普

【问题讨论】:

    标签: java web-services client-server java-web-start


    【解决方案1】:

    我最终创建了一个非常简单的 Web 服务,可以列出给定路径的所有根文件夹或所有子文件夹。 现在由客户端来拥有一个(GUI)浏览器来访问此服务。

    package net.pms.plugin.webservice.filesystem;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    import net.pms.plugin.webservice.ServiceBase;
    
    @WebService(serviceName = "FileSystem", targetNamespace = "http://ps3mediaserver.org/filesystem")
    public class FileSystemWebService extends ServiceBase {
    
        @WebMethod()
        public List<String> getRoots() {
            List<String> roots = new ArrayList<String>();
            for(File child : File.listRoots()) {
                roots.add(child.getAbsolutePath());
            }
            return roots;
        }
    
        @WebMethod()
        public List<String> getChildFolders(@WebParam(name="folderPath") String folderPath) throws FileNotFoundException {
            List<String> children = new ArrayList<String>();
            File d = new File(folderPath);
            if(d.isDirectory()) {
                for(File child : d.listFiles()) {
                    if(child.isDirectory() && !child.isHidden()) {
                        children.add(child.getAbsolutePath());
                    }
                }
            } else {
                throw new FileNotFoundException();
            }
            return children;
        }
    }
    

    对于想要使用它的人,这里也是 ServiceBase 类

    package net.pms.plugin.webservice;
    
    import javax.xml.ws.Endpoint;
    
    import org.apache.log4j.Logger;
    
    public abstract class ServiceBase {
        private static final Logger log = Logger.getLogger(ServiceBase.class);
        protected boolean isInitialized;
    
        /**
         * the published endpoint
         */
        private Endpoint endpoint = null;
    
        /**
         * 
         * Start to listen for remote requests
         * 
         * @param host ip or host name
         * @param port port to use
         * @param path name of the web service
         */
        public void bind(String host, int port, String path) {
            String endpointURL = "http://" + host + ":" + port + "/" + path;
            try {
                endpoint = Endpoint.publish(endpointURL, this);
                isInitialized = true;
                log.info("Sucessfully bound enpoint: " + endpointURL);
            } catch (Exception e) {
                log.error("Failed to bind enpoint: " + endpointURL, e);
            }
        }
    
        /**
         * Stop the webservice
         */
        public void shutdown() {
            log.info("Shut down " + getClass().getName());
            if (endpoint != null)
                endpoint.stop();
            endpoint = null;
        }
    
    }
    

    【讨论】:

    • 更加更加优雅。你能详细说明一下上下文吗?
    • 谢谢。客户端必须有更多的智能^,因为它必须自己创建文件夹树,但这是解决方案固有的,客户端可以使用不同的技术(java、net、web、rich ...)。我需要能够从可能在远程机器上运行的客户端配置共享文件夹(位于服务器上)。这是您根据上下文所说的吗?
    • 间接;我将javax.jwsjws 部分误认为是Java Web Start,而不是Java(基于XML)Web 服务。我将添加web-services 标签。
    【解决方案2】:

    从客户端,您也许可以利用smbclient -L 的输出。在服务器上,一个合适的servlet 可能会做。

    【讨论】:

    • 似乎应该有更优雅的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多