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

应用场景:

        需要文件服务器,向应用提供基于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();
//	}
}


转载请注明出处!谢谢!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值