繁体   English   中英

与客户端连接时如何浏览服务器计算机的文件系统(java)

[英]How to browse the file system of a server machine when connecting with a client (java)

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

在服务器端,所有服务都已使用javax.jws公开,并且客户端代理已使用wsimport生成。

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

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

我以为我会找到一个提供此功能的库,但找不到任何库。 使用UNC路径浏览文件并访问远程计算机似乎不可行,因为这对用户而言不是透明的。

现在,我不想担心安全性问题,只要其余的看起来可行,我就会解决这些问题。

我将不胜感激。 谢谢菲利普

我最终创建了一个非常简单的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;
    }

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM