springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件
springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样
@RequestMapping("/uploadPicture")
@ResponseBody
public JSONResultUtil uploadPicture(MultipartFile file, Integer viewId) {
if (file == null) {
return JSONResultUtil.error("文件没接到");
}
logger.debug("file -> {},viewId ->{}", file.getOriginalFilename(), viewId); String fileOriName = file.getOriginalFilename();// 获取原名称
String fileNowName = UUIDUtil.getUUID2() + "." + FilenameUtils.getExtension(fileOriName);// 生成唯一的名字
try {
FileHandleUtil.uploadSpringMVCFile(file, fileNowName); Picture picture = new Picture();
picture.setCreatetime(new Date());
picture.setName(fileOriName);
picture.setPath(fileNowName);
picture.setViewId(viewId);
pictureService.addPicture(picture);
} catch (Exception e) {
logger.error("uploadPicture error", e);
return JSONResultUtil.error("添加景点图片出错");
} return JSONResultUtil.ok();
}
保存文件到本地的方法如下:
public static boolean uploadSpringMVCFile(MultipartFile multipartFile, String fileName) throws Exception {
String fileDir = StringUtils.defaultIfBlank(FileHandleUtil.getValue("path", "picture"), "E:/picture/"); if (!new File(fileDir).exists()) {
new File(fileDir).mkdirs();
}
multipartFile.transferTo(new File(fileDir + fileName));// 保存文件 return true;
}
这个默认的有文件上传大小的限制,默认是1MB,可以用下面配置进行修改:
########设置文件上传大小的限制
#multipart.maxFileSize=10Mb是设置单个文件的大小, multipart.maxRequestSize=100Mb是设置单次请求的文件的总大小
#如果是想要不限制文件上传的大小,那么就把两个值都设置为-1就行
spring.http.multipart.maxFileSize = 10Mb
spring.http.multipart.maxRequestSize=100Mb
2. 不配置虚拟路径访问服务器的图片等文件
参考:https://www.cnblogs.com/qlqwjy/p/9510878.html
后台代码:
@RequestMapping("/getPicture")
public void getPicture(HttpServletRequest request, HttpServletResponse response, String path) {
FileInputStream in = null;
ServletOutputStream outputStream = null;
try {
File fileByName = FileHandleUtil.getFileByName(path);
in = new FileInputStream(fileByName);
outputStream = response.getOutputStream();
IOUtils.copyLarge(in, outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(outputStream);
}
}
创建File对象的代码:
public static File getFileByName(String path) {
String fileDir = StringUtils.defaultIfBlank(FileHandleUtil.getValue("path", "picture"), "E:/picture/");
return new File(fileDir + path);
}
前端可以访问此路径并且传一个path,如下:(thymeleaf语法)
<img alt="" th:src="${'/picture/getPicture.html?path='+picture.path}" height="300px" width="300px"/>
3. 配置日期的格式化格式
有时候希望日期类型的字段转JSON的时候采用特定的格式,如下:
############################################################
#
# 格式化日期类型为JSON的格式
#
############################################################
spring.jackson.date-format=yyyy-MM-dd
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false
4. Springboot配置日期转换器
有时候需要将前台的日期格式的字符串自动转换为Date类型,不加转换器会报错,所以需要增加转换器,方法如下:
package cn.qs.config; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /**
* 1.日期转换
*
* @author Administrator
*
*/
@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter { @Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
} /**
* 日期转换类
*
* @author Administrator
*
*/
private class DateConverter implements Converter<String, Date> {
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override
public Date convert(String s) {
if ("".equals(s) || s == null) {
return null;
}
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
}
springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器的更多相关文章
- Spring Boot 嵌入式 Tomcat 文件上传、url 映射虚拟路径
1.Java web 应用开发完成后如果是导入外置的 Tomcat 的 webapps 目录的话,那么上传的文件可以直接的放在应用的 web 目录下去就好了,浏览器可以很方便的进行访问. 2.Spri ...
- SpringBoot上传文件到本服务器 目录与jar包同级问题
目录 前言 原因 实现 不要忘记 最后的封装 Follow up 前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...
- springBoot上传文件时MultipartFile报空问题解决方法
springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...
- SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案
SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...
- springboot 项目打包部署后设置上传文件访问的绝对路径
1.设置绝对路径 application.properties的配置 #静态资源对外暴露的访问路径 file.staticAccessPath=/upload/** #文件上传目录(注意Linux和W ...
- SpringBoot 上传文件如何获取项目工程路径
上传文件时,需要将上传的文件存放于工程路径中,以便前端能够获取文件资源,那如何获取工程路径呢? //获取 SpringBoot 工程中 static 的绝对路径 String serverpath= ...
- SpringBoot上传文件到本服务器 目录与jar包同级
前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...
- Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件
springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...
- spring-boot上传文件MultiPartFile获取不到文件问题解决
1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现 multipartRequest.ge ...
随机推荐
- 001_ jQuery的表格插件dataTable详解
一. 1.启用id为"datatable1"标签的html的表格jQuery库 $("#datatable1").dataTable( ) Reference: ...
- Redis常用数据结构
Redis常用数据结构包括字符串(strings),列表(lists),哈希(hashes),集合(sets),有序集合(sorted sets). redis的key最大不能超过512M,可通过re ...
- Struts2的核心——拦截器
虽然以前已经学了很多的拦截器,但是在这里还是想重头梳理一下所有有关拦截器的知识,尤其是struts2中的拦截器 1:拦截器是什么? java里的拦截器是动态拦截Action调用的对象.它提供了一种机制 ...
- vertical-align和text-align属性实现垂直水平居中
HTML: <div class="box"> <div class="content"> <span class="s ...
- Python中的 一些常用技巧函数[.join()]
1.str.join(item)字符串操作函数,参数item可以是字符串.元组.字典,示例 ','.join('abc') [','.join('abc')] 输出: 'a,b,c'['a', 'b' ...
- 下载图片没有关闭http输入流导致下载超时
在某次接入第三方厂商数据时,需要根据对方提供的URL地址下载图片,当数据量大时会遇到很多的下载图片超时问题,开始以为是第三方厂商的问题,对方排查了很久之后,说是我这边下载数据全部留在缓存区,导致缓存区 ...
- 第六十八天 js轮播图
1.浮动与定位结合使用 浮动与相对定位 //1.两者均参与布局 //2.主浮动布局,相对布局辅助完成布局微调 //3.相对定位布局微调不同于盒模型布局微调,相对定位布局不影响盒子原有位置,就会影响兄弟 ...
- Java基础-1
基础知识 1.进制 1.十进制 2.二进制 3.十六进制 2.十六进制转换 二进制转换 十进制转换
- go实现json数组嵌套
go实现json数组嵌套 引用包 "encoding/json" 定义以下结构体 type person struct { Name string `json:"name ...
- CF1059D Nature Reserve
原题链接 网络不好的可以到洛谷上去QwQ 题目大意 有N个点,求与y=0相切的,包含这N个点的最小圆的半径 输入输出样例 输入: 2 0 1 1 1 输出 0.625 感觉最多是蓝题难度? 首先无解的 ...