1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径

SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径

时间:2020-03-13 06:12:13

相关推荐

SpringBoot 存储图片 tomcat服务器存图片 数据库图片路径

前言:SpringBoot 图片存储存放域名+图片路径访问

/uploadimage/06165dc6fd50-3432-4a50-a5c4-14a55713993f.jpg

存储图片到数据库里 这里分两种方式

将图片保存的路径存储到数据库(图片存放在服务器,服务器的图片路径存储在数据库)将图片以二进制数据流的形式直接写入数据库字段中(base64的形式)

配置application.properties:

MultipartFile是Spring上传文件的封装类,包含了文件的二进制流和文件属性等信息,在配置文件中也可对相关属性进行配置,基本的配置信息如下:

Spring Boot 2.0之后(注意Mb大小写不同)

spring.servlet.multipart.enabled=true#默认支持文件上传.spring.servlet.multipart.file-size-threshold=0#支持文件写入磁盘.spring.servlet.multipart.location=# 上传文件的临时目录spring.servlet.multipart.max-file-size=1Mb# 最大支持文件大小spring.servlet.multipart.max-request-size=10Mb# 最大支持请求大小

最常用的是一下配置内容,限制文件上传大小,上传时超过大小会抛出异常:

spring.servlet.multipart.max-file-size=500MB # 最大支持文件大小spring.servlet.multipart.max-request-size=500MB # 最大支持请求大小

工具类如下:

import org.springframework.web.multipart.MultipartFile;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;public class ImageUtil {//存放图片的绝对路径private final static String SAVE_IMAGE_PATH = "L:/UploadImage/";/*** 返回文件后缀* @param file* @return*/public static String getImagePath(MultipartFile file) {String fileName = file.getOriginalFilename();//获取原文件名int index = fileName.indexOf(".");return fileName.substring(index, fileName.length());}/*** 保存图片* @param mfile* @param file* @return*/public static boolean saveImage(MultipartFile mfile , File file) {//查看文件夹是否存在,不存在则创建if(!file.getParentFile().exists()) {file.getParentFile().mkdirs();}try {//使用此方法保存必须要绝对路径且文件夹必须已存在,否则报错mfile.transferTo(file);return true;} catch (IllegalStateException | IOException e) {e.printStackTrace();}return false;}/*** 新文件名* @param suffix* @return*/public static String getNewFileName(String suffix) {SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String date = sdf.format(new Date());return date + UUID.randomUUID() + suffix;}/*** 返回图片保存地址* @param name* @return*/public static String getNewImagePath(String name) {return SAVE_IMAGE_PATH+name;}}

controller层:

package com.lt.crm.controller;import com.mon.util.ImageUtil;import io.swagger.annotations.Api;import io.swagger.annotations.ApiResponse;import io.swagger.annotations.ApiResponses;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;@RestController@RequestMapping("/Image")@Api(value = "图片", tags = "文件图片")public class UploadController {@ApiResponses({@ApiResponse(code = 200, message = "上传成功"), @ApiResponse(code = 400, message = "上传失败"), @ApiResponse(code = 500, message = "服务器内部错误")})@RequestMapping(value = "/uploadimage", method = RequestMethod.POST)public Map<String, Object> addimage(@RequestParam("files") MultipartFile[] files) {Map<String, Object> map = new HashMap<>();List<String> list = new ArrayList<>();for (int i = 0; i < files.length; i++) {MultipartFile mfile = files[i];//获取文件后缀String suffixName = ImageUtil.getImagePath(mfile);//生成新文件名称String newFileName = ImageUtil.getNewFileName(suffixName);//保存文件File file = new File(ImageUtil.getNewImagePath(newFileName));boolean state = ImageUtil.saveImage(mfile, file);if (state) {// list.add(ImageUtil.getNewImagePath(newFileName));//保存数据库的图片路径为 相对路径list.add("uploadimage/" + newFileName);}}map.put("imgList", list);return map;}}

postman如何测试?

1. 选择Body>form-date>File

2. 输入KEY 选择图片

3.本地找我们设置的图片路径(如果是部署tomcat服务器 找对应的地址, 一般都会放到webapps)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。