当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合TomCat实现本地图片服务器代码解析

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

2020年08月17日  | 移动技术网IT编程  | 我要评论
后台控制层: public static final string head_img_dir = "d:/upload/"; // 本地存放图片路径 //图片上传 @requestmapping

后台控制层:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网