-
springboot项目图片上传到本地tomcat图片服务器
2020-12-02 16:02:37springboot项目图片上传到本地tomcat图片服务器 本地tomcat图片服务器 在host节点下增加节点 <Context docBase="D://picture" path="/picture" /> docBase:为图片保存的路径 path:为项目访问路径...springboot项目图片上传到本地tomcat图片服务器
本地tomcat图片服务器
在host节点下增加节点
<Context docBase="D://picture" path="/picture" />
- docBase:为图片保存的路径
- path:为项目访问路径(http://localhost:8081/picture/)
- 修改tomcat安装目录conf下的web.xml文件,servlet(default)节点下的listings的value改为true
path:为项目访问路径(http://localhost:8081/picture/)
修改tomcat安装目录conf下的web.xml文件
servlet(default)节点下的listings的value改为true
controller层
package com.example.doublecontrol.utils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; @Controller @CrossOrigin public class PictureUpload { @Value("${static.url}") private String staticUrl;//D://picture(本地图片保存地址) @Value("${static.addressurl}") private String addressurl;//http://localhost:8081/picture/(本地tomcat访问地址) @RequestMapping("uploadimg") @ResponseBody public Map<String, Object> upload(MultipartFile file) { Map<String, Object> map = new HashMap(); Map<String, Object> map2 = new HashMap(); String realPath = staticUrl + "//" + file.getOriginalFilename(); File dest = new File(realPath); try { if(!dest.getParentFile().exists()){ dest.getParentFile().mkdir(); } try { //保存文件 file.transferTo(dest); } catch (IllegalStateException e) { // TODO Auto-generated catch block System.out.println(e); e.printStackTrace(); } String resUrl = this.addressurl+file.getOriginalFilename();//前台获取图片的地址 map.put("code", 0); map.put("msg", "上传成功"); map2.put("src", resUrl); map2.put("title", file.getOriginalFilename()); map.put("data", map2); }catch (Exception e){ System.out.println(e); map.put("code", 1); map.put("msg", e); map2.put("src", ""); map2.put("title",""); map2.put("data", ""); } return map; } @RequestMapping("/Picturetest") public String login() { return "Welcome to picture server."; } }
-
Java访问Tomcat图片资源
2020-12-15 14:47:25Java访问Tomcat图片资源 环境 CentOS Linux release 8.1.1911 (Core) java 14.0.2 2020-07-14 Java(TM) SE Runtime Environment (build 14.0.2+12-46) Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, ...Java访问Tomcat图片资源
环境
CentOS Linux release 8.1.1911 (Core) java 14.0.2 2020-07-14 Java(TM) SE Runtime Environment (build 14.0.2+12-46) Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing) apache-tomcat-9.0.38
实现代码
读取
Tomcat
主目录下./webapps/ROOT
目录下默认存在的图片tomcat.png
其中,
<host_name>
为服务器主机名(IP地址),PORT
为端口号,Tomcat
默认使用8080
,使用阿里云、腾讯云等服务器需要在安全组配置中设置内网入方向规则package com.demo.tomcat; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Tomcat { private static String HOST_NAME = "http://<host name>"; private static int PORT = 8080; public void getImage(String filePath) { String path = String.format(HOST_NAME + ":%d/", PORT) + filePath; URL url = null; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(true); if (httpURLConnection.getResponseCode() == 200) { InputStream inputStream = httpURLConnection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("./res/tomcat.jpg"); System.out.println("Request succeed!"); saveImage(fileOutputStream, inputStream); System.out.println("Save Image succeed!"); } else { System.out.println("Request failed!"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void saveImage(FileOutputStream fileOutputStream, InputStream inputStream) { int length; byte buffer[] = new byte[1024]; while (true) { try { if ((length = inputStream.read(buffer)) == -1) { break; } fileOutputStream.write(buffer, 0, length); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { Tomcat tomcat = new Tomcat(); tomcat.getImage("tomcat.png"); } }
测试结果
路径
./res/tomcat.png
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
-
搭建tomcat图片服务器
2018-05-03 14:37:09搭建tomcat图片服务器只需两步:(1)修改tomcat端口(2)配置路径找到conf文件夹下的server.xml,按需修改配置文件,然后启动tomcat服务器即可。修改后的server.xml文件如下:<?xml version='1.0' encoding=...搭建tomcat图片服务器只需两步:
(1)修改tomcat端口
(2)配置路径
找到conf文件夹下的server.xml,按需修改配置文件,然后启动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 --> <!-- 8005修改为8006 --> <Server port="8006" 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 --> <!-- 8080修改为8081,8443修改为8444 --> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" /> <!-- 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 --> <!-- 8009修改为8010,8443修改为8444 --> <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" /> <!-- 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" /> --> <!-- 配置路径,docBase是图片存储的地址 --> <Context path="/img" docBase="/usr/local/pic" reloadable="false"></Context> <!-- 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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
运行结果如下:
-
tomcat图片验证码报错
2019-01-29 17:30:31tomcat图片验证码报错 场景:在linux服务器上,java后端生成图片验证码。后台报错找不到类 解决方案: 1.修改catalina.sh JAVA_OPTS增加参数-Djava.awt.headless=true JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS -D...tomcat图片验证码报错
- 场景:在linux服务器上,java后端生成图片验证码。后台报错找不到类
- 解决方案:
1.修改catalina.sh
JAVA_OPTS增加参数-Djava.awt.headless=trueJAVA_OPTS="$JAVA_OPTS $JSSE_OPTS -Djava.awt.headless=true"
2.重新启动生效
-
Tomcat图片服务器的配置用法详解
2020-06-02 22:37:01Tomcat图片服务器 1.创建图片服务器。 将tomcat复制一份解压。解压后的文件夹名字后加上 -file 此时,该文件夹下的tomcat为图片服务器。 2.创建图片储存目录。 打开图片服务器根目录\webapps\(新建文件夹用于存放... -
IDEA 中tomcat图片储存和访问虚拟路径
2018-12-20 10:15:31IDEA 中tomcat图片储存和访问虚拟路径idea部署Tomcat(IDEA 中tomcat图片储存和访问虚拟路径) IDEA 中tomcat图片储存和访问虚拟路径) idea部署Tomcat(IDEA 中tomcat图片储存和访问虚拟路径) 原文链接:... -
tomcat 图片服务器
2018-05-17 15:23:331.需要两个tomcat --apache-tomcat-8.0.37和apache-tomcat-8.0.37-2:apache-tomcat-8.0.37 用来跑项目,apache-tomcat-8.0.37-2用来当图片服务器2.改变apache-tomcat-8.0.37-2的端口号(1)打开D:\apache-tomcat-... -
java tomcat 图片_JAVA怎样把图片从A tomcat上传B tomcat;
2021-03-06 06:46:24JAVA怎样把图片从A tomcat上传B tomcat;JAVA怎样把图片从A tomcat上传B tomcat;www.someabcd.com网友分享于:Jun 6, 2018 11:37:17 PMHttpClient http://blog.sina.com.cn/s/blog_978a39e4010120fg.html 链接提示... -
tomcat图片缓存问题
2011-03-20 00:57:14[size=medium][size=medium][size=large][size=medium] 最近学jsp编程,写了个jsp页面,纠结了好久,原因是tomcat图片缓存的问题一直没有解决。 后来查了些资料,知道每次启动jsp页面时,tomcat服务器会首先将工程... -
idea热部署 tomcat图片 设置虚拟访问路径
2018-09-30 18:55:57idea热部署 tomcat图片 设置虚拟访问路径 通过两种方式: 第一种:在idea软件里,Run菜单按钮的Edit Configurations中,选择你的tomcat,点击Deployment,按“+”符号添加你想要映射的图片文件目录,然后修改... -
Tomcat 图片虚拟路径映射
2017-12-12 15:02:19我们可以将外部资源如图片、文档等映射到tomcat中以便使用,那么tomcat如何让访问图片路径映射到磁盘路径? 答:tomcat虚拟图片路径配置,通常设置虚拟目录的目的是将tomcat的安装和项目的保存分开。 ... -
web的tomcat图片失败
2019-12-30 22:09:52我就直接重新把以前的图片删除,然后将图片重新上传一遍。 网上还有其他的做法: 1.https://blog.csdn.net/zouh613/article/details/50512698 2.(1) 将tomcat/webapps/目录清空,删掉该文件夹里面的全部文件。 ... -
Linux tomcat图片服务器
2014-11-12 17:33:20Linux不是很熟,本来打算是配置 Linux + nginx + tomcat ,静态文件,图片资源等通过nginx处理的,但是配置了半天,nginx没有配置成功,无奈,只能先用tomcat 建另外一个图片服务器先顶着:按照下文一步一步走的,... -
Eclipse配置TomCat图片讲述,超简单!
2018-11-28 17:47:36如何在Eclipse中配置Tomcat呢? 找到配置Tomcat的位置 配置Tomcat 配置JDK 这个就是刚才我们配置好的Tomcat,点击...显示以下图片表示启动成功 显示这个绿色图标表示Tomcat在运行了 在网页中测试一下:成功! ... -
解决idea上传图片重启tomcat图片消失的问题
2019-08-17 16:01:43以前一直是用eclipse开发,后来第一次用idea,做图片上传时,以如下的方法创建图片的存储路径: String savePath=request.getSession().getServletContext().getRealPath("/")+"images/uploadImg"; System.out.... -
在Eclipse里配置tomcat图片路径
2017-09-22 16:41:41版权声明:本文为博主原创...原因大概是我在Eclipse下引入tomcat的时候,设置了Server Loactions下的Use Workspace metadata而不是Use Tomcat instaltion. 在前者这个选项下,我们可以看到连个无法编辑的子选项,Serv -
安卓客户端加载tomcat图片遇到的一些问题
2017-01-09 13:39:13我的Android studio 已经得到了tomcat的图片路径(多个)。显示图片的控件是GridLayout。这些字符串如何处理可以避免oom,同时可以带有缓存,差点忘了,这个GridLayout是在listView item,最好加载速度可以快点,... -
IDEA 配置tomcat图片储存和访问虚拟路径
2019-09-30 09:02:011.编辑服务器的配置信息 ...2.编辑tomcat的conf 在tomcat的confi中的server.xml中配置 <Context path="/file" docBase="D:\project\train" reloadable="true" crossContext="true"/> 3.配置成功 ... -
tomcat图片问题解决方法
2012-04-26 16:29:50Linux下tomcat处理图片时需加上参数“-Djava.awt.headless=true” linux下,如果抛出 Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of ... -
springmvc+tomcat图片上传后如何立即刷新出来
2017-11-05 18:27:291、上传图片,配置相关文件,略 2、配置tomcat ...3、配置好自后图片上传就能立即自动显示4、(修改tomcat配置文件配置方法,没有测试过)参考链接:http://blog.csdn.net/brooksyao/article/deta -
Tomcat 图片服务器的配置
2014-04-18 16:45:27这里仅需要在Tomcat类服务器中简单配置图片上传的地址即可,在Tomcat配置文件server.xml中添加配置,如把D:/uploads比作应用服务器,配置如下: 比如updates下有一张1.jpg的图片,我们可以通过... -
安卓tomcat图片下载,关于发送json的问题
2016-02-10 22:51:27universal-image-loader接收图片的同时下载comments和description,只是用一个url,返回json,请问可以做到么? -
Tomcat图片的上传到本地仓库并且完成页面回显
2020-03-15 21:44:55必须要在form表单中添加enctype="multipart/form-data来保证可以传输图片 设置method="post"来保证项目的运行 <form action="${pageContext.request.contextPath}/uploadController/upload.do" method="post... -
上传图片,重启tomcat图片就不见了。
2013-07-25 08:51:24我的上传路径是这样获取的 saveurl=request.getSession().getServletContext().getRealPath("/") + url; 数据库保存的是相对地址。上传到tomcat下,可是过几天图片就自动消失了。求解决方法 -
Tomcat 图片不能显示的问题
2010-10-01 18:44:00今天发现Tomcat 对 png格式的图片支持不太好,一些图能显示,另一些却不能显示。 我把所有的 png图片换成 GIF, (html代码里一定要用大写的GIF)显示图片就正常了! 大写GIF !很高兴,很兴奋。
收藏数
8,738
精华内容
3,495