约定优于配置。无web.xml,默认采用是以jar包形式启动嵌入式的servlet容器,从而启动Spring Boot 的应用。
创建项目
https://start.spring.io/
导入项目import existing maven project
端口号被占用可以在application.properties中设置
server.port=7777
默认不适用jsp
但可以在pom中导入jasper文件,version继承父类
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
jstl
热启动
org.springframework.boot
spring-boot-devtools
日期转换器
@Component
public class CustomDateConverter implements Converter{
@Override
public Date convert(String arg0) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(arg0);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
测试
@RestController
@RequestMapping("/param")
public class ParamController {
@RequestMapping("/test")
public String test(Student s) {
System.out.println(s.getStuid());
System.out.println(s.getStuname());
System.out.println(s.getBirthday());
return "{\"result\":true}";
}
}
配置视图解析器application.properties
#配置视图解析器的前缀和后缀
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
配置静态资源
#配置静态资源
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,file:d:/images
#修改文件上传大小限制 ( 默认1M)
#单个文件大小限制
spring.servlet.multipart.max-file-size=10MB
#多个文件总大小限制(file控制支持多选的时候)
spring.servlet.multipart.max-request-size=20MB
上传文件
@RestController
public class UploadController {
//spring boot 文件上传,如果没有引入commons-fileupload,就按照servlet3.0的方式上传
@RequestMapping("/upload")
public String upload(MultipartFile avatarfile) {
//1.新的文件名
String filename=UUID.randomUUID().toString()+avatarfile.getOriginalFilename();
//2.拷贝到d:/images
File dest=new File("d:/images",filename);
try {
avatarfile.transferTo(dest);
return "{\"file\":"+filename+"}";
} catch (IllegalStateException |IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.保存数据库
//4.返回文件路径
return null;
}
@RequestMapping("/upload1")
public String upload1(MultipartFile[] avatarfile) {
for(MultipartFile item:avatarfile) {
//1.新的文件名
String filename=UUID.randomUUID().toString()+item.getOriginalFilename();
//2.拷贝到d:/images
File dest=new File("d:/images",filename);
try {
item.transferTo(dest);
} catch (IllegalStateException |IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//3.保存数据库
//4.返回文件路径
}
return "{\"result\":true}";
}
html文件
Insert title here
文件下载
@Controller
public class DownloadController {
@RequestMapping("/download")
public void download(HttpServletResponse response) {
//文件名中如果有中文,需要按utf-8编码
String filename=null;
try {
filename=URLEncoder.encode("中文.zip","utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//设置响应头
//设置响应的content-type(MIME类型):二进制(application/octet-stream)
response.setHeader("Content-Type", "application/octet-stream");
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
try {
// 得到输入流
FileInputStream in = new FileInputStream("d:/images/28c2b481-46ed-470d-9155-f606cb31d17dNotePad_v7.6.1.zip");
BufferedInputStream bin = new BufferedInputStream(in);
// 得到输出流
OutputStream out = response.getOutputStream();
BufferedOutputStream bout = new BufferedOutputStream(out);
byte[] b = new byte[1024];
int len = bin.read(b);
while (len !=-1) {
bout.write(b, 0, len);
len = bin.read(b);
bout.flush();
}
bout.close();
bin.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}