tomcat或Jboss实现图片上传至服务器并共享访问,虚拟路径设置方法


有时候我们为了节约服务器磁盘空间资源,需要把所有上传的文件都上传到指定的另外一台服务器,即跟部署的应用不在同一台服务器,对于普通的文件倒没什么,但是对于图片,图片上传后,还需要能加载访问,这时候,就需要用到虚拟路径设置方法来实现访问了。其实这种方式也是部署应用的方式之一。


可参考 http://blog.csdn.net/liangbinny/article/details/6272448 方法2



具体设置方法是:


在tomcat中的conf目录中,在server.xml中的, <host/ >节点中添加: 
<Context crossContext="true" docBase="\\127.0.0.1\uploads" path="/uploadfiles" reloadable="true"></Context>

其中path就是访问路径,127.0.0.1就是你要上传的另外一台服务器ip


应用访问图片方式为:

 <img src="/uploadfilepath/2015-01/370e94aea35d4b0d9cea6baaae897a8f.jpg"  WIDTH="200" HEIGHT="200" BORDER="0" ALT=""/> 

JBoss方式也一样:

JBoss修改%JBoss%/server/default/deploy/jboss-web.deployer下的server.xml即可,方法一样。



Jetty方式的话,需要配置jetty.xml文件,内容如下:


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<!-- =============================================================== -->
<!-- Configure the Jetty Server                                      -->
<!--                                                                 -->
<!-- Documentation of this file format can be found at:              -->
<!-- http://docs.codehaus.org/display/JETTY/jetty.xml                -->
<!--                                                                 -->
<!-- =============================================================== -->


<Configure id="Server" class="org.mortbay.jetty.Server">

	<!-- =========================================================== -->
	<!-- Server Thread Pool                                          -->
	<!-- =========================================================== -->
	<Set name="ThreadPool">
		<!-- Default bounded blocking threadpool 
			  -->
		<New class="org.mortbay.thread.BoundedThreadPool">
			<Set name="minThreads">10</Set>
			<Set name="maxThreads">250</Set>
			<Set name="lowThreads">25</Set>
		</New>

		<!-- Optional Java 5 bounded threadpool with job queue 
			  <New class="org.mortbay.thread.concurrent.ThreadPool">
				<Set name="corePoolSize">250</Set>
				<Set name="maximumPoolSize">250</Set>
			  </New>
			  -->
	</Set>



	<!-- =========================================================== -->
	<!-- Set connectors                                              -->
	<!-- =========================================================== -->
	<!-- One of each type!                                           -->
	<!-- =========================================================== -->

	<!-- Use this connector for many frequently idle connections
			 and for threadless continuations.
		-->    
	<!-- 用eclipse内嵌jetty,已经配好项目的话,这个无需,这个是用于一个jetty启动多个项目使用
	<Call name="addConnector">
		<Arg>
			<New class="org.mortbay.jetty.nio.SelectChannelConnector">
				<Set name="port">
					<SystemProperty name="jetty.port" default="8080"/>
				</Set>
				<Set name="maxIdleTime">30000</Set>
				<Set name="Acceptors">2</Set>
				<Set name="statsOn">false</Set>
				<Set name="confidentialPort">8443</Set>
				<Set name="lowResourcesConnections">5000</Set>
				<Set name="lowResourcesMaxIdleTime">5000</Set>
			</New>
		</Arg>
	</Call>
	-->

	<!-- Use this connector if NIO is not available.
		<Call name="addConnector">
		  <Arg>
			  <New class="org.mortbay.jetty.bio.SocketConnector">
				<Set name="port">8081</Set>
				<Set name="maxIdleTime">50000</Set>
				<Set name="lowResourceMaxIdleTime">1500</Set>
			  </New>
		  </Arg>
		</Call>
		-->

	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<!-- To add a HTTPS SSL listener                                     -->
	<!-- see jetty-ssl.xml to add an ssl connector. use                  -->
	<!-- java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml             -->
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    
	<!-- =========================================================== -->
	<!-- Set up global session ID manager                            -->
	<!-- =========================================================== -->
	<!--
		<Set name="sessionIdManager">
		  <New class="org.mortbay.jetty.servlet.HashSessionIdManager">
			<Set name="workerName">node1</Set>
		  </New>
		</Set>
		-->

	<!-- =========================================================== -->
	<!-- Set handler Collection Structure                            --> 
	<!-- =========================================================== -->
	<Set name="handler">
		<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
			<Set name="handlers">
				<Array type="org.mortbay.jetty.Handler">
					<Item>
						<New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
					</Item>
					<Item>
						<New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
					</Item>
					<Item>
						<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
					</Item>
				</Array>
			</Set>
		</New>
	</Set>
    
	<Set name="handler">  
		<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">  
			<Set name="handlers">  
				<Array type="org.mortbay.jetty.Handler">  
					<Item>  
						<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>  
					</Item>  
					<Item>  
						<New class="org.mortbay.jetty.webapp.WebAppContext">  
							<Set name="contextPath">/uploadfiles</Set>  <!-- 虚拟地址,访问http://xxx/uploadfiles实际访问resourceBase节点配置的地方-->
							<Set name="resourceBase">\\127.0.0.1\uploads</Set>  <!-- 访问虚拟地址映射的磁盘路径 -->
							<Call name="addServlet">  
								<Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>  
								<Arg>/uploadfiles</Arg>  
							</Call>  
						</New>  
					</Item> 
					<Item>  
						<New class="org.mortbay.jetty.webapp.WebAppContext">  
							<Set name="contextPath">/SmartCity</Set>  <!--项目地址-->
							<Set name="resourceBase">WebRoot</Set> <!--项目路径地址-->
							<Call name="addServlet">  
								<Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>  
								<Arg>/SmartCity</Arg>  
							</Call>  
							<Set name="defaultsDescriptor">WebRoot/WEB-INF/webdefault.xml</Set> <!-- 自定义设置webdefault.xml是为了允许服务启动后修改静态文件,主要修改了webdefault.xml里的useFileMappedBuffer参数设置为false-->
						</New>  
					</Item>  
				</Array>  
			</Set> 
		</New>  
	</Set>
 

	<!-- =========================================================== -->
	<!-- Configure Authentication Realms                             -->
	<!-- Realms may be configured for the entire server here, or     -->
	<!-- they can be configured for a specific web app in a context  -->
	<!-- configuration (see $(jetty.home)/contexts/test.xml for an   -->
	<!-- example).                                                   -->
	<!-- =========================================================== -->
	<Set name="UserRealms">
		<Array type="org.mortbay.jetty.security.UserRealm">
			<!--
					<Item>
					  <New class="org.mortbay.jetty.security.HashUserRealm">
						<Set name="name">Test Realm</Set>
						<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
					  </New>
					</Item>
				-->
		</Array>
	</Set>

	<!-- =========================================================== -->
	<!-- Configure Request Log                                       -->
	<!-- Request logs  may be configured for the entire server here, -->
	<!-- or they can be configured for a specific web app in a       -->
	<!-- contexts configuration (see $(jetty.home)/contexts/test.xml -->
	<!-- for an example).                                            -->
	<!-- =========================================================== -->
	<!--Ref id="RequestLog">
		  <Set name="requestLog">
			<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
			  <Set name="filename"><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Set>
			  <Set name="filenameDateFormat">yyyy_MM_dd</Set>
			  <Set name="retainDays">90</Set>
			  <Set name="append">true</Set>
			  <Set name="extended">true</Set>
			  <Set name="logCookies">false</Set>
			  <Set name="LogTimeZone">GMT</Set>
			</New>
		  </Set>
		</Ref-->

	<!-- =========================================================== -->
	<!-- extra options                                               -->
	<!-- =========================================================== -->
	<Set name="stopAtShutdown">true</Set>
	<Set name="sendServerVersion">true</Set>
	<!--Set name="sendDateHeader">true</Set-->
	<!--Set name="gracefulShutdown">1000</Set-->
</Configure>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值