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配置日期转换器的更多相关文章

  1. Spring Boot 嵌入式 Tomcat 文件上传、url 映射虚拟路径

    1.Java web 应用开发完成后如果是导入外置的 Tomcat 的 webapps 目录的话,那么上传的文件可以直接的放在应用的 web 目录下去就好了,浏览器可以很方便的进行访问. 2.Spri ...

  2. SpringBoot上传文件到本服务器 目录与jar包同级问题

    目录 前言 原因 实现 不要忘记 最后的封装 Follow up   前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...

  3. springBoot上传文件时MultipartFile报空问题解决方法

    springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...

  4. SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案

    SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...

  5. springboot 项目打包部署后设置上传文件访问的绝对路径

    1.设置绝对路径 application.properties的配置 #静态资源对外暴露的访问路径 file.staticAccessPath=/upload/** #文件上传目录(注意Linux和W ...

  6. SpringBoot 上传文件如何获取项目工程路径

    上传文件时,需要将上传的文件存放于工程路径中,以便前端能够获取文件资源,那如何获取工程路径呢? //获取 SpringBoot 工程中 static 的绝对路径 String serverpath= ...

  7. SpringBoot上传文件到本服务器 目录与jar包同级

    前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...

  8. Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

    springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...

  9. spring-boot上传文件MultiPartFile获取不到文件问题解决

    1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现 multipartRequest.ge ...

随机推荐

  1. go笔记-pprof使用

    go tool pprof http://localhost:6060/debug/pprof/profile go tool pprof http://localhost:6060/debug/pp ...

  2. iOS开发基础-九宫格坐标(4)

    对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...

  3. js字符串转时间

    function StrToDateTime(value) { if (value) { return (new Date(Date.parse(value.replace(/-/g, "/ ...

  4. matplotlib绘图的基本操作

    转自:Laumians博客园 更简明易懂看Matplotlib Python 画图教程 (莫烦Python)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili. ...

  5. 判断语句之if..else if...else

    判断语句之if..else if...else if语句第三种格式:if..else if...else 格式: 执行流程 首先判断关系表达式1看其结果是true还是false 如果是true就执行语 ...

  6. redis cli命令

    redis安装后,在src和/usr/local/bin下有几个以redis开头的可执行文件,称为redis shell,这些可执行文件可做很多事情. 可执行文件 作用 redis-server  启 ...

  7. C#中字符串的字面值(转义序列)

    在程序开发中,经常会碰到在字符串中字面值中使用转义序列,下面表格收集了下转义序列的完整列表,以便大家查看引用: 转义序列列表 转义序列 产生的字符 字符的Unicode值 \' 单引号 0x0027 ...

  8. 一些有意思的Linux命令

    1.输出你最常用的十条命令 history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort - ...

  9. CSS代码检查工具stylelint

    前面的话 CSS不能算是严格意义的编程语言,但是在前端体系中却不能小觑. CSS 是以描述为主的样式表,如果描述得混乱.没有规则,对于其他开发者一定是一个定时炸弹,特别是有强迫症的人群.CSS 看似简 ...

  10. js实现一键导出Excel

    演示地址:https://xibushijie.github.io/static/ExportToExcel.html <!DOCTYPE html> <html lang=&quo ...