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;
}
}
}