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

后台控制层:

 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配置文件,在文件中加上以下代码


  
   
		 
		 		
				  
		 
			 
			 
					
				 
			   
			 
		 
	
 

前台页面:

url: 'http://127.0.0.1:8020/',

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

你可能感兴趣的:(SpringBoot整合TomCat实现本地图片服务器代码解析)