使用Springboot上传图片并将URL保存到数据库中 |
您所在的位置:网站首页 › php上传图片到数据库完整代码是什么 › 使用Springboot上传图片并将URL保存到数据库中 |
背景:之前一直想做上传图片如何将图片保存到服务器路径下面并将URL地址保存到数据库中,在网上找了很多大多都是五五开,有幸找到一篇简单的上传图片教程,并对其加以改正,得到了自己想要的效果。
参考博主链接:https://blog.csdn.net/Coding13/article/details/54577076 废话不多说,先将我自己项目的整体结构展示一下 application.yml配置 server.port=12000 ##jdbc配置 mysql 8.1以上版本 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/btn-dev spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.username=root spring.datasource.password=123456 ##jdbc配置 mysql 8.1以下版本 #spring.datasource.driver-class-name=com.mysql.jdbc.Driver #spring.datasource.url=jdbc:mysql://localhost:3306/btn #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #spring.datasource.username=root #spring.datasource.password=123456 ## 该配置节点为独立的节点,有容易将这个配置放在spring的节点下,导致配置无法被识别 #注意:一定要对应mapper映射xml文件的所在路径 mybatis.mapper-locations=classpath:com/library/nbt/dao/**/mapper/*.xml mybatis.type-aliases-package=com.library.nbt.model.entity #配置generatorConfig.xml 运行时:mybatis-generator:generate -e db.driverLocation=D:\\Interest\\mybatis-generator-core-1.3.2\\lib\\mysql-connector-java-8.0.12.jar #本地浏览器地址 spring.web.googleexcute=explorer #要打开的网址 spring.web.loginurl=http://localhost:${server.port}/swagger-ui.html #是否要启动时打开浏览器 spring.web.isopenurl=true logging.config=classpath:log/dev/logback.xml file.rootPath=E: file.sonPath=/img/ 配置类 package com.library.nbt.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { //图片存放根路径 @Value("${file.rootPath}") private String ROOT_PATH; //图片存放根目录下的子目录 @Value("${file.sonPath}") private String SON_PATH; @Override public void addResourceHandlers(ResourceHandlerRegistry registry){ String filePath = "file:" + ROOT_PATH + SON_PATH; //指向外部目录 registry.addResourceHandler("img//**").addResourceLocations(filePath); super.addResourceHandlers(registry); } } service层及实现类 package com.library.nbt.service; import org.springframework.web.multipart.MultipartFile; public interface UploadFileService { String getUploadFilePath(MultipartFile file); } package com.library.nbt.service.impl; import com.library.nbt.dao.UplocalFileEntityMapper; import com.library.nbt.model.entity.UplocalFileEntity; import com.library.nbt.service.UploadFileService; import com.library.nbt.utils.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Random; @Service public class UploadFileServiceImpl implements UploadFileService { private static final Logger LOG = LoggerFactory.getLogger(UploadFileServiceImpl.class); //图片存放根路径 @Value("${file.rootPath}") private String ROOT_PATH; //图片存放根目录下的子目录 @Value("${file.sonPath}") private String SON_PATH; @Value("${server.port}") //获取主机端口 private String POST; @Autowired private UplocalFileEntityMapper uplocalFileEntityMapper; @Override public String getUploadFilePath(MultipartFile file) { //返回上传的文件是否为空,即没有选择任何文件,或者所选文件没有内容。 //防止上传空文件导致奔溃 if (file.isEmpty()) { throw new NullPointerException("文件为空"); } // 设置文件上传后的路径 String filePath = ROOT_PATH + SON_PATH; // 获取文件名后缀名 String suffix = file.getOriginalFilename(); String prefix = suffix.substring(suffix.lastIndexOf(".")+1); //为防止文件重名被覆盖,文件名取名为:当前日期 + 1-1000内随机数 Random random = new Random(); Integer randomFileName = random.nextInt(1000); String fileName = DateUtils.timeStamp2Date(String.valueOf(System.currentTimeMillis() /100),"yyyyMMddHHmmss") + randomFileName +"." + prefix; //创建文件路径 File dest = new File(filePath + fileName); // 解决中文问题,liunx下中文路径,图片显示问题 // fileName = UUID.randomUUID() + suffixName; // 检测是否存在目录 if (!dest.getParentFile().exists()) { //假如文件不存在即重新创建新的文件已防止异常发生 dest.getParentFile().mkdirs(); } try { //transferTo(dest)方法将上传文件写到服务器上指定的文件 file.transferTo(dest); //保存t_upload_file表中 String filePathNew = SON_PATH + fileName; String profilePhoto = saveUploadFile(filePathNew); System.out.println(profilePhoto); return profilePhoto; } catch (Exception e) { return dest.toString(); } } private String saveUploadFile(String filePathNew) { //获取本机IP String host = null; try { host = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { LOG.error("get server host Exception e:", e); } UplocalFileEntity uplocalFileEntity = new UplocalFileEntity(); uplocalFileEntity.setCreateTime(System.currentTimeMillis()); uplocalFileEntity.setUpdateTime(System.currentTimeMillis()); uplocalFileEntity.setEnabled(1); uplocalFileEntity.setProfilePhoto(host + ":" + POST + filePathNew); Integer result = uplocalFileEntityMapper.insertSelective(uplocalFileEntity); System.out.println("插入了" + result + "数据"); System.out.println("uplocalFileEntity.getProfilePhoto():" + uplocalFileEntity.getProfilePhoto()); return uplocalFileEntity.getProfilePhoto(); } } mapper层及实现 package com.library.nbt.dao; import com.library.nbt.model.entity.UplocalFileEntity; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UplocalFileEntityMapper { /** * This method was generated by MyBatis Generator. * This method corresponds to the database table t_uplocal_file * * @mbggenerated */ int insert(UplocalFileEntity record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table t_uplocal_file * * @mbggenerated */ int insertSelective(UplocalFileEntity record); } SELECT LAST_INSERT_ID() insert into t_uplocal_file (create_time, update_time, enabled, profile_photo) values (#{createTime,jdbcType=BIGINT}, #{updateTime,jdbcType=BIGINT}, #{enabled,jdbcType=INTEGER}, #{profilePhoto,jdbcType=VARCHAR}) SELECT LAST_INSERT_ID() insert into t_uplocal_file create_time, update_time, enabled, profile_photo, #{createTime,jdbcType=BIGINT}, #{updateTime,jdbcType=BIGINT}, #{enabled,jdbcType=INTEGER}, #{profilePhoto,jdbcType=VARCHAR}, 进行测试,我用的是SwaggerUI,如果没有安装的话可以下载Postman进行接口测试 ![]()
url也保存到数据库中了,复制URL地址在网页打开即可访问 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |