[英]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.