欢迎来到代码驿站!

当前位置:首页 >

SpringBoot整合TomCat实现本地图片服务器代码解析

时间:2020-08-14 10:00:09|栏目:|点击:

后台控制层:

 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;">

上一篇:在vue中封装方法以及多处引用该方法详解

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:SpringBoot整合TomCat实现本地图片服务器代码解析

本文地址:http://www.codeinn.net/misctech/3525.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有