1. 文件上传
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<!--1.表单提交默认的enctype为application/x-www-form-urlencoded:数据以字符串拼接的方式发送
2.如果要涉及到文件上传,则需要改成multipart/formdata:不对字符进行编码
3.text/plain:将空格转换成+,对其他的特殊字符不进行编码-->
<form action="/upload/fileUpload" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="username"><br>
文件:<input type="file" name="uploadfile"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
/**
* @author muzi@softeem.com
* @description 文件上传
* @since 2022/1/19 20:21
*/
@RestController
@RequestMapping("/upload")
public class FileUploadController {
private static final String BASE_PATH = "C:\\Users\\Administrator\\Desktop\\vip4-
upload\\";
@PostMapping("fileUpload")
// public String fileUpload(String username, HttpServletRequest request){
public String fileUpload(String username, MultipartFile uploadfile){
// MultipartHttpServletRequest mhsr = (MultipartHttpServletRequest) request;
// MultipartFile file = mhsr.getFile("uploadfile");
MultipartFile file = uploadfile;
System.out.println(file);
System.out.println("文件的大小:"+file.getSize());
System.out.println("文件的名字:"+file.getName());
System.out.println("文件的类型:"+file.getContentType());
System.out.println("文件的原始名字:"+file.getOriginalFilename());
String originalFilename = file.getOriginalFilename();
//文件的后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
System.out.println("文件的后缀为:"+suffix);
//文件存储,并改变文件的名字(改成时间戳)存储
String filename = "用户头像" + System.currentTimeMillis() + suffix;
// try {
// //拿到文件输入流
// InputStream inputStream = file.getInputStream();
// FileOutputStream fileOutputStream = new FileOutputStream(BASE_PATH +filename);
// int b = 0;
// while((b=inputStream.read())!=-1){
// fileOutputStream.write(b);
// }
// } catch (IOException e) {
// e.printStackTrace();
// System.err.println("文件存储失败!!");
// }
try {
file.transferTo(new File(BASE_PATH + filename));
} catch (IOException e) {
e.printStackTrace();
System.err.println("文件存储失败!!");
}
return "Success";
}
}
<!-- 文件上传处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件上传的字符集 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 一次请求上传的文件大小限制,默认为-1代表不做限制,单位是byte -->
<property name="maxUploadSize" value="500000"/>
<!-- 每一个文件的大小都不能超过100k -->
<property name="maxUploadSizePerFile" value="100000"/>
</bean>
2. 对时间格式的处理
2.1 请求带时间
1. 局部解决方案 @DateTimeFormat
public String timeReceive(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday)
或者在包装类属性上加
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
默认情况下,SpringMVC能帮你将以
/
作为分隔符的时间字符串转换成Date对象
如果不是/
作为分隔,默认会报错
2. 全局配置
/**
* @author muzi@softeem.com
* @description 自定义格式转换器
* @since 2022/1/19 21:25
*/
public class MyDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date myTime = null;
try {
myTime = simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return myTime;
}
}
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.softeem.converter.MyDateConverter"/>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
2.2 响应带时间
局部解决方案 @JsonFormat
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date birthday;
全局解决方案
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg name="pattern" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
实际上底层的步骤是:
ObjectMapper objectMapper = new ObjectMapper();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
objectMapper.setDateFormat(simpleDateFormat);
try {
//String dateString = simpleDateFormat.format(new Date());
String dateString = objectMapper.writeValueAsString(new Date());
} catch (JsonProcessingException e) {
e.printStackTrace();
}