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 ...
随机推荐
- Mapreduce数据分析实例
数据包 百度网盘 链接:https://pan.baidu.com/s/1v9M3jNdT4vwsqup9N0mGOA提取码:hs9c 复制这段内容后打开百度网盘手机App,操作更方便哦 1. ...
- 将代码托管到github服务器之SSH验证
内容中包含 base64string 图片造成字符过多,拒绝显示
- vue 2.0 + ElementUI构建树形表格
解决: 本来想在网上博客找一找解决方法,奈何百度到的结果都不尽人意,思维逻辑不清,步骤复杂,代码混乱,找了半天也没找到一个满意的,所以干脆就自己动手写一个 思路: table需要的数据是array,所 ...
- Java多线程10:join()方法
一.前言 通过一个简单的例子引入join()方法 public class Thread01 extends Thread{ @Override public void run() { for(int ...
- MySQL-ERROR 2003
1.首先安装mysqld服务器,输入命令:mysqld --install 2.输入命令:mysqld --initialize-insecure 3.输入命令:net start mysql
- SpringMVC DispatcherServlet在配置Rest url-pattern的一点技巧
SpringMVC的Controller中已经有了@RequestMapping(value = "detail.do", method = RequestMethod.GET)的 ...
- 为什么要写 tf.Graph().as_default()
首先,去tensorflow官网API上查询 tf.Graph() 会看到如下图所示的内容: 总体含义是说: tf.Graph() 表示实例化了一个类,一个用于 tensorflow 计算和表示用的数 ...
- position:fixed not work?
问题 在position:fixed的使用中,突然发现某个操作之后,fixed定位的位置变了?? bottom:0,left:0.本来应该在最下面,结果跑没影了. wtf?position:fixed ...
- Excel将一列数据变为两列
如下表可将第一列分散到第二列和第三列 A B C 1 =OFFSET($A$1,(ROW(A1)-1)*2+COLUMN(A1)-1,) &"" =OFFSET($A$2, ...
- 继续沿用旧的网络访问模式Apache HTTP 客户端,防止Android9闪退
注意位置,在application 节点里面.