SpringBoot整合TomCat实现本地图片服务器代码解析
作者:李加减 时间:2021-07-30 16:34:19
后台控制层:
public static final String HEAD_IMG_DIR = "D:/upload/"; // 本地存放图片路径
//图片上传
@RequestMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) {
//文件真实上传名字
String filename = file.getOriginalFilename();
//文件大小
Long size = file.getSize();
String contentType = file.getContentType();
//文件临时储存到本地
String folder = HEAD_IMG_DIR;
//生成保存的文件名字,这个名字要存到数据库中
String uuid = UUID.randomUUID().toString();
try {
file.transferTo(new File(folder + uuid));
} catch (IOException e) {
e.printStackTrace();
}
return uuid; // 返回给前台 uuid 需和信息一起存到数据库
}
Tomcat:
打开server.xml配置文件,在文件中加上以下代码
<!-- 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
-->
<!--配置TomCat本地服务器-->
<Service name="newtest">
<!--分配8020端口 -->
<Connector port="8020"
protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="GBK"
redirectPort="8443" />
<Engine name="newtest" defaultHost="localhost">
<!--name为项目访问地址 此配置的访问为http://localhost:8020 appBase配置tomcat下wabapps下的路径-->
<Host name="localhost" appBase="D://TomCat//webapps" unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!--资源地址--> <!-- 就是访问http://localhost:8020这个地址就是到D://upload这个目录下 -->
<Context path="" docBase="D://upload" debug="0" reloadable="false"/>
</Host>
</Engine>
</Service>
<Service name="Catalina">
前台页面:
url: 'http://127.0.0.1:8020/',
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
来源:https://www.cnblogs.com/lzf199/p/13432758.html
标签:Spring,Boot,整合,TomCat,服务器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java8中的 Lambda表达式教程
2023-10-13 01:32:29
java selenium教程环境搭建基于Maven
2023-11-27 01:35:38
java9新特性Collection集合类的增强与优化方法示例
2023-01-29 05:16:57
![](https://img.aspxhome.com/file/2023/4/61534_0s.png)
Java流程控制之循环结构for,增强for循环
2023-11-03 10:45:20
![](https://img.aspxhome.com/file/2023/4/58834_0s.jpg)
MyBatis超详细讲解如何实现分页功能
2023-08-22 23:06:51
![](https://img.aspxhome.com/file/2023/6/58216_0s.png)
Java利用移位运算将int型分解成四个byte型的方法
2023-11-09 08:25:00
![](https://img.aspxhome.com/file/2023/4/59334_0s.png)
Mybatis初始化知识小结
2023-11-01 13:59:27
![](https://img.aspxhome.com/file/2023/8/58708_0s.png)
SpringBoot基于Sentinel在服务上实现接口限流
2023-11-27 17:19:25
![](https://img.aspxhome.com/file/2023/4/59574_0s.jpg)
Spring5中的WebClient使用方法详解
2023-08-05 14:50:24
Java+MySQL实现学生信息管理系统源码
2023-11-28 04:29:31
![](https://img.aspxhome.com/file/2023/6/59866_0s.jpg)
深入解析java中的locale
2023-11-09 18:14:20
java中Scanner输入用法实例
2023-09-03 18:25:36
![](https://img.aspxhome.com/file/2023/5/58315_0s.png)
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
Thread类interrupt interrupted及isInterrupted区别
2023-07-20 06:52:20
![](https://img.aspxhome.com/file/2023/8/57688_0s.png)
Java语言读取配置文件config.properties的方法讲解
2023-09-29 14:45:51
![](https://img.aspxhome.com/file/2023/9/86759_0s.png)
手把手教你JAVA进制之间的转换
2023-11-14 23:18:56
![](https://img.aspxhome.com/file/2023/7/59417_0s.png)
java模拟hibernate一级缓存示例分享
2023-06-18 08:43:55
Java实现简易Web服务器
2023-11-17 09:10:11
Java的封装类和装箱拆箱详解
2023-09-20 22:41:22
Spring Boot整合Web项目常用功能详解
2023-06-04 17:14:21