您的位置:首页 > 理论基础 > 计算机网络

创建文件服务器的几种方法(将本地目录发布为网络路径)

2014-11-17 16:46 656 查看

应用场景:

需要文件服务器,向应用提供基于http协议的文件服务,让应用可以通过url访问资源。

PS:归根到底是要提供http服务,同时将本地目录映射到指定的url下,有点类似linux的软链接,文件的物理目录未发生改变,只是为本地目录提供了基于http协议的访问方式。

搭建方法:

方法1、使用 apache http发布

linux默认安装http组件,windows下也可以自己下载

下载地址:http://httpd.apache.org/download.cgi

需要改的就是httpd.conf这个配置文件,添加如下代码:

Alias /namespace/ "C:/yourDir/"


namespace 是 相对于WebRoot的名称,即url中http://ip:port/后的相对路径;

yourDir 就是 你本地想要发布的根目录咯。

方法2、使用类似Tomcat这样的servlet容器发布

有两个位置可以用来配置映射目录,核心关键词都是Context

A:在conf目录下的server.xml中,文档最下面的 <host>标签内,可以添加一个Context 节点,

<Context docBase="C:\yourdir\" path="/namespace" reloadable="true" />

该方式与方法1类似。若要映射根目录,将path设定为空,即path=""。

B:在conf\Catalina\localhost 目录中添加一个xml,这个xml的内容与A中所示内容相同

<Context docBase="Z:/yourDir/" privileged="true" antiResourceLocking="false" antiJARLocking="false" reloadable="true">


但是namespace 则作为文件名,例如该xml文件名为fileserver,则访问url为http://ip:port/fileserver/就是你设定的映射目录。

方法3、编程发布

我使用的是jetty,不过不是用jetty做servlet容器那样修改配置文件发布,而是借鉴了jetty的嵌入式开发模式,即使用API中提供的Endpoint。

我的应用场景是:

一个后台程序定时生成日志,客户想直接在网页上看到日志内容,因此我直接将后台程序根目录下的loginfo目录发布到
http://127.0.0.1:18089/loginfo下,这样自动生成的日志可以在这个url下直接访问。当然为了显示简单,我日志是字符串拼接成html格式再写文件,这样便于控制显示样式。
package com.system.util;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.Properties;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

import com.adc.quicktools.PropertiesUtil;
import com.adc.quicktools.StringUtil;
import com.adc.quicktools.file.FileControl;

public class LogDetailUtil {
public final static String logDir="/loginfo";
public final static String Key_ServiceHost="service.host";
public final static String DefaultServiceHost="127.0.0.1";

private static String loginfoTempPath = System.getProperty("user.dir")+ logDir+"/";

private static Thread logServer;

public static void startLogServer() {
if (logServer == null) {
logServer = new Thread() {

public void run() {
this.setName("LogFileServer");
publishService();
}
};
logServer.start();
}
}

private static void publishService() {
String hostIp=getHostInfo().getProperty(Key_ServiceHost);
if(StringUtil.checkStringValue(hostIp)){
hostIp=DefaultServiceHost;
}
Server server = new Server(new InetSocketAddress(hostIp, 18089));
WebAppContext webContext = new WebAppContext("loginfo", logDir);
server.setHandler(webContext);
System.out.println("loginfo server is starting...");
try {
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}

private static Properties hostInfo;

public static Properties getHostInfo() {
if (hostInfo==null||hostInfo.isEmpty()) {
String filePath=getRuntimeConfigDir().concat("/com/system/util/host.properties");
hostInfo=PropertiesUtil.loadProperties(filePath);
}
return hostInfo;
}
private static String runtimeCfgDir;
public static String getRuntimeConfigDir(){
if(!StringUtil.checkStringValue(runtimeCfgDir)){
runtimeCfgDir=getRuntimeBaseDir().concat("/");
}
return runtimeCfgDir;
}
private static String runtimeBaseDir;

public static String getRuntimeBaseDir(){
if(!StringUtil.checkStringValue(runtimeBaseDir)){
runtimeBaseDir=new File(FileControl.getAppPath(LogDetailUtil.class)).getPath().replace("\\", "/").concat("/");
}
return runtimeBaseDir;
}
//	public static void main(String[] args) {
//		publishService();
//	}
}


转载请注明出处!谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: