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. day 12 装饰器

    nonlocal关键字 # 作用:将 L 与 E(E中的名字需要提前定义) 的名字统一​# 应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值​# 案例:​def outer():    n ...

  2. spring boot拦截器中获取request post请求中的参数(转)

    文章转自 https://www.jianshu.com/p/69c6fba08c92

  3. ansible 与 Jinja2的结合

    1.文件架构 [root@master template]# tree . ├── jinj2_test.yml ├── meta ├── tasks ├── templates │   └── te ...

  4. java中内存分配

    java程序运行时内存分配详解  一. 基本概念 每运行一个java程序会产生一个java进程,每个java进程可能包含一个或者多个线程,每一个Java进程对应唯一一个JVM实例,每一个JVM实例唯一 ...

  5. SharedPreferences类的使用

    SharedPreferences,用xml文件保存用户的偏好设置,是一个轻量级的存储类. 效果图: 代码: activity_main <?xml version="1.0" ...

  6. tomcat知识(一)

    1.tomcat配置javaWeb项目常见错误: ①:端口占用 ②:未配置JAVA_HOME环境变量 2.tomcat修改端口号 tomcat安装路径下面找到conf文件夹,修改server.xml文 ...

  7. 一、Swagger配置

    一.Swagger配置 1.注解不显示 SwaggerConfig文件下   //c.IncludeXmlComments(GetXmlCommentsPath()):  内下面添加: c.Inclu ...

  8. EntityManagerFactory 是多线程的 将其变成一个单线程(使用静态方法)提交效率

    由于EntityManagerFactory 是一个线程安全的对象(即多个线程访问同一个EntityManagerFactory 对象不会有线程安全问题),并且EntityManagerFactory ...

  9. CentOS 7安装MongoDB

    1 下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.4.tgz 2 解压 .tgz 3 将解压包 ...

  10. 4.1 socket

    socket  背景概念 脑图结构 OSI 模型 socket 概念特性 脑图结构 理解示意图  额外补充 Socket是应用层与 TCP/IP协议族通信的中间软件抽象层,它是一组接口. 在设计模式中 ...