问题描述我在上传图片的位置不在Tomcat服务器下,用户无法访问

解决方案:配置Tomcat虚拟路径使用户可以访问图片

配置Tomcat

# cd /usr/local/apache-tomcat-7.0.67/conf/  //切换到Tomcat下的配置文件

# vi server.xml   //编辑Tomcat 的server.xml文件

找到Host节点,在Host节点下加入如下代码:

<Context path="访问路径" docBase="文件夹路径" debug="debug的等级" reloadable="热加载状态"/>
//修改完成后,按ESC退出编辑模式
//按Shift+q 切到控制台模式
//输入wq保存修改信息

例子:

  <Context path="/test" docBase="/usr/img" debug="0" reloadable="true" crosscontext="true" />

  debug="0"        : debug 取值为0时,提供最少的debug信息,取值为9时,提供最多的debug信息

  path=“/test”      : path为访问路径,http://localhost:8080/test/1.jpg

  docBase="/usr/img"   : docBase为项目路径,比如,我只想让这个路径能获取图片,那么就设置为图片所在的路径

  reloadable="true"       : reloadable=true时 当web.xml或者class有改动的时候都会自动重新加载不需要从新启动服务

  crosscontext="true"   :表示配置的不同context共享一个session

以上配置的项目路径或其他的虚拟路径,端口号是一样的    http://localhost:8080/test/1.jpg

下面是我的Tomcat server.xml文件的配置的截图 和代码

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
--> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
--> <!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
--> <!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
--> <!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm> <Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<Context path="/file/user" docBase="/usr/img/user" debug="0" reloadable="true"/>
<Context path="/file" docBase="/usr/java/" debug="0" reloadable="true"/>
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host>
</Engine>
</Service>
</Server>

  

Linux下设置Tomcat虚拟路径的更多相关文章

  1. IDEA 设置 TOMCAT 虚拟路径

    今天在使用 IDEA 配置 TOMCAT 虚拟路径时一直报错,最终解决方式整理如下: 一.使用 Tomcat 自己的虚拟路径 1.在 Tomcat9\conf 目录下找到 server.xml 文件, ...

  2. idea设置tomcat虚拟路径的两种方法

    1.使用tomcat自己的虚拟路径 1.1.在tomcat\config\server.xml中配置 path="/upload" 虚拟路径 E:\photo\upload 图片存 ...

  3. 如何在idea中设置Tomcat虚拟路径

    设置项目的根路径: 设置指定文件的在Tomcat中的虚拟路径: 代码: String fileName = MyFileUtil.getFileName(uploadFileName); File f ...

  4. linux下设置tomcat自启动

    怎么设置linux安装了tomcat之后让tomcat开机就启动呢? 下来我们来简单的说一下: 第一步@1: 首先我们找到tomcat的安装的位置,找到之后我们cd到tomcat的bin目录下面; 我 ...

  5. Linux下设置Tomcat开机自启动

    --未验证 第一步:在/etc/init.d下新建一个文件tomcat(需要root操作权限) vi /etc/init.d/tomcat 然后点击"i"写下如下代码,tomcat ...

  6. Linux下设置Tomcat开机启动

    1.进入/etc/rc.d/init.d,新建文件tomcat,并让它成为可执行文件 chmod 755 tomcat. #!/bin/bash # # /etc/rc.d/init.d/tomcat ...

  7. 48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)

    设置tomcat虚拟路径的两种方法(Eclipse.tomcat.IDEA) 三种方式设置虚拟服务器路径如果我们要实现一个上传文件的功能,但是又想要上传的文件不会随着自己web服务器的重启而不能访问了 ...

  8. Linux 下配置Tomcat的虚拟路径

    如果你的Linux服务器下,不止一个tomcat的时候,这个时候,你就会发现,每次去发布项目很麻烦,还需要到webapps下面去看,繁琐的很,这里就用到了,Tomcat的虚拟路径,制定一个目录,作为t ...

  9. Linux下配置Tomcat服务器

    Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar.gz格式的即可,下面使用命令行的方式安 ...

随机推荐

  1. 斗地主[NOIP2015]

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  2. ASP.NET 导出excel文件出现乱码的解决办法

    string html =TABLE ;//<table>标签,可以是多张表string modified = Regex.Replace(html, "<table &g ...

  3. Windows下搭建Git 服务器: BONOBO GIT SERVER + TortoiseGit

    本文将介绍如何在Windows操作系统下搭建Git服务器和客户端.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配 ...

  4. POJ 2524 Ubiquitous Religions 解题报告

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34122   Accepted:  ...

  5. VsCode 附加Chorme调试TS方法

    命令行进入:[谷歌浏览器安装目录/]/chrome.exe --remote-debugging-port=9222 关闭所有窗口并结束Chorme进程(如果无法附加调试做此操作,可以正常调试请忽略. ...

  6. JavaScript闭包,只学这篇就够了

    # 闭包不是魔法 这篇文章使用一些简单的代码例子来解释JavaScript闭包的概念,即使新手也可以轻松参透闭包的含义. 其实只要理解了核心概念,闭包并不是那么的难于理解.但是,网上充斥了太多学术性的 ...

  7. mysql explain和profiling

    语法:explain  select .... 变体: 1.explain extended select .... 将执行计划“反编译”成select语句: 运行show warnings 可以得到 ...

  8. 关于div+css排版布局中需注意的细节问题

    第一个注意点:选择器的使用(标签.class.id) 三种选择器中id(#)的优先级最高,根据id名筛选出唯一元素: 如下输入:#menu{ width:1200px; height:45px; ba ...

  9. JavaScript高级程序设计 - 阅读笔记

    [本博客为原创:http://www.cnblogs.com/HeavenBin/] 前言: 大致花费了一个星期的时间把这本书认真看了半本,下面是我做的阅读笔记,希望能够让看这本书的人有个大致的参考. ...

  10. MATLAB中绘制图形的时候,坐标和标题倒置

    1.如上图所示,直方图的坐标轴以及标题文字都颠倒了 原因: 在MATLAB显示的subplot函数中,图像与直方图这些不属于一类,所以在显示的时候会出现这种情况 解决办法:1>将图像与直方图分开 ...