全局日期处理、文件上传下载、参数绑定、restful请求
This commit is contained in:
parent
c3428d4056
commit
e038b16ddd
@ -46,6 +46,39 @@
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.54</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>27.0.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--spring 的文件上传类 CommonsMultipartResolver 依赖了这个包-->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<!-- 数据校验依赖包 -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.13.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :测试restful风格的实体类
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class Pet {
|
||||
|
||||
|
||||
private String ownerId;
|
||||
|
||||
private String petId;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
public class Programmer {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Min(value = 0,message = "年龄不能为负数!" )
|
||||
private int age;
|
||||
|
||||
@Min(value = 0,message = "薪酬不能为负数!" )
|
||||
private float salary;
|
||||
|
||||
private String birthday;
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import com.heibaiying.convert.CustomDateConverter;
|
||||
import com.heibaiying.exception.NoAuthExceptionResolver;
|
||||
import com.heibaiying.interceptors.MyFirstInterceptor;
|
||||
import com.heibaiying.interceptors.MySecondInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
@ -58,4 +61,23 @@ public class ServletConfig implements WebMvcConfigurer {
|
||||
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
|
||||
resolvers.add(new NoAuthExceptionResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加全局日期处理
|
||||
*/
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addConverter(new CustomDateConverter());
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置文件上传
|
||||
*/
|
||||
@Bean
|
||||
public CommonsMultipartResolver multipartResolver(){
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
resolver.setMaxUploadSize(1024*1000*10);
|
||||
resolver.setMaxUploadSizePerFile(1024*1000);
|
||||
resolver.setDefaultEncoding("utf-8");
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,96 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
|
||||
import com.heibaiying.utils.FileUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 文件上传
|
||||
*/
|
||||
|
||||
@Controller
|
||||
public class FileController {
|
||||
|
||||
@GetMapping("file")
|
||||
public String filePage() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 单文件上传
|
||||
*/
|
||||
@PostMapping("upFile")
|
||||
public String upFile(MultipartFile file, HttpSession session) {
|
||||
FileUtil.saveFile(file, session.getServletContext().getRealPath("/image"));
|
||||
return "success";
|
||||
}
|
||||
|
||||
/***
|
||||
* 多文件上传 多个文件用同一个名字
|
||||
*/
|
||||
@PostMapping("upFiles")
|
||||
public String upFiles(@RequestParam(name = "file") MultipartFile[] files, HttpSession session) {
|
||||
for (MultipartFile file : files) {
|
||||
FileUtil.saveFile(file, session.getServletContext().getRealPath("images"));
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
/***
|
||||
* 多文件上传方式2 分别为不同文件指定不同名字
|
||||
*/
|
||||
@PostMapping("upFiles2")
|
||||
public String upFile(String extendParam,
|
||||
@RequestParam(name = "file1") MultipartFile file1,
|
||||
@RequestParam(name = "file2") MultipartFile file2, HttpSession session) {
|
||||
String realPath = session.getServletContext().getRealPath("images2");
|
||||
FileUtil.saveFile(file1, realPath);
|
||||
FileUtil.saveFile(file2, realPath);
|
||||
System.out.println("extendParam:" + extendParam);
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("upFileForDownload")
|
||||
public String upFileForDownload(MultipartFile file, HttpSession session, Model model) throws UnsupportedEncodingException {
|
||||
String path = FileUtil.saveFile(file, session.getServletContext().getRealPath("/image"));
|
||||
model.addAttribute("filePath", URLEncoder.encode(path,"utf-8"));
|
||||
model.addAttribute("fileName", file.getOriginalFilename());
|
||||
return "fileDownload";
|
||||
}
|
||||
|
||||
/***
|
||||
* 下载文件
|
||||
*/
|
||||
@GetMapping("download")
|
||||
public ResponseEntity<byte[]> downloadFile(String filePath) throws IOException {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
File file = new File(filePath);
|
||||
// 解决文件名中文乱码
|
||||
String fileName=new String(file.getName().getBytes("UTF-8"),"iso-8859-1");
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
headers.setContentDispositionFormData("attachment", fileName);
|
||||
|
||||
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
|
||||
headers, HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.datetime.DateFormatter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :参数绑定
|
||||
*/
|
||||
@Controller
|
||||
public class ParamBindController {
|
||||
|
||||
/*@InitBinder
|
||||
protected void initBinder(WebDataBinder binder) {
|
||||
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
|
||||
}*/
|
||||
|
||||
|
||||
// 参数绑定与日期格式转换
|
||||
@RequestMapping("param")
|
||||
public String param(String name, int age, double salary, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birthday, Model model) {
|
||||
model.addAttribute("name", name);
|
||||
model.addAttribute("age", age);
|
||||
model.addAttribute("salary", salary);
|
||||
model.addAttribute("birthday", birthday);
|
||||
return "param";
|
||||
}
|
||||
|
||||
@RequestMapping("param2")
|
||||
public String param2(String name, int age, double salary, Date birthday, Model model) {
|
||||
model.addAttribute("name", name);
|
||||
model.addAttribute("age", age);
|
||||
model.addAttribute("salary", salary);
|
||||
model.addAttribute("birthday", birthday);
|
||||
return "param";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("param3")
|
||||
public String param3(Programmer programmer, String extendParam, Model model) {
|
||||
System.out.println("extendParam" + extendParam);
|
||||
model.addAttribute("p", programmer);
|
||||
return "param";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :数据校验
|
||||
*/
|
||||
@RestController
|
||||
public class ParamValidController {
|
||||
|
||||
|
||||
@PostMapping("validate")
|
||||
public void valid(@Validated Programmer programmer,
|
||||
BindingResult bindingResult) {
|
||||
List<ObjectError> allErrors = bindingResult.getAllErrors();
|
||||
for (ObjectError error : allErrors) {
|
||||
System.out.println(error.getDefaultMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Pet;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Restful 风格的请求
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class RestfulController {
|
||||
|
||||
|
||||
@GetMapping("restful/owners/{ownerId}/pets/{petId}")
|
||||
public void get(@PathVariable String ownerId, @PathVariable String petId) {
|
||||
System.out.println("ownerId:" + ownerId);
|
||||
System.out.println("petId:" + petId);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("restful2/owners/{ownerId}/pets/{petId}")
|
||||
public void get(@ModelAttribute Pet pet) {
|
||||
System.out.println("ownerId:" + pet.getOwnerId());
|
||||
System.out.println("petId:" + pet.getPetId());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.heibaiying.convert;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public class CustomDateConverter implements Converter<String, Date> {
|
||||
|
||||
public Date convert(String s) {
|
||||
try {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return simpleDateFormat.parse(s);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.heibaiying.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 文件上传工具类
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
public static String saveFile(MultipartFile file, String path) {
|
||||
String fullPath = path + File.separator + file.getOriginalFilename();
|
||||
try {
|
||||
File saveDir = new File(path);
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
FileOutputStream outputStream = new FileOutputStream(new File(fullPath));
|
||||
InputStream inputStream = file.getInputStream();
|
||||
byte[] bytes = new byte[1024 * 1024];
|
||||
int read;
|
||||
while ((read = inputStream.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, read);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>文件上传</title>
|
||||
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/file.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFile" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件:<input name="file" type="file"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFiles" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件(多选):<input name="file" type="file" multiple><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFiles2" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件1:<input name="file1" type="file"><br>
|
||||
请选择上传文件2:<input name="file2" type="file"><br>
|
||||
文件内容额外备注: <input name="extendParam" type="text"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFileForDownload" method="post" enctype="multipart/form-data">
|
||||
上传并下载:<input name="file" type="file"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>文件下载</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="${pageContext.request.contextPath}/download?filePath=${filePath}">${fileName}</a>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,15 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Restful</title>
|
||||
<script src="${pageContext.request.contextPath}/js/jquery3.3.1.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
<li>姓名:${empty name ? p.name : name}</li>
|
||||
<li>年龄:${empty age ? p.age : age}</li>
|
||||
<li>薪酬:${empty salary ? p.salary : salary}</li>
|
||||
<li>生日:${empty birthday ? p.birthday : birthday}</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
操作成功!
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,3 @@
|
||||
form {
|
||||
border: 1px green dashed;
|
||||
}
|
10364
spring/springmvc-base-annotation/src/main/webapp/js/jquery3.3.1.js
Normal file
10364
spring/springmvc-base-annotation/src/main/webapp/js/jquery3.3.1.js
Normal file
File diff suppressed because it is too large
Load Diff
@ -55,6 +55,29 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>27.0.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--spring 的文件上传类 CommonsMultipartResolver 依赖了这个包-->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<!-- 数据校验依赖包 -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.13.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :测试restful风格的实体类
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class Pet {
|
||||
|
||||
|
||||
private String ownerId;
|
||||
|
||||
private String petId;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
public class Programmer {
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Min(value = 0,message = "年龄不能为负数!" )
|
||||
private int age;
|
||||
|
||||
@Min(value = 0,message = "薪酬不能为负数!" )
|
||||
private float salary;
|
||||
|
||||
private String birthday;
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
|
||||
import com.heibaiying.utils.FileUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 文件上传
|
||||
*/
|
||||
|
||||
@Controller
|
||||
public class FileController {
|
||||
|
||||
@GetMapping("file")
|
||||
public String filePage() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 单文件上传
|
||||
*/
|
||||
@PostMapping("upFile")
|
||||
public String upFile(MultipartFile file, HttpSession session) {
|
||||
FileUtil.saveFile(file, session.getServletContext().getRealPath("/image"));
|
||||
return "success";
|
||||
}
|
||||
|
||||
/***
|
||||
* 多文件上传 多个文件用同一个名字
|
||||
*/
|
||||
@PostMapping("upFiles")
|
||||
public String upFiles(@RequestParam(name = "file") MultipartFile[] files, HttpSession session) {
|
||||
for (MultipartFile file : files) {
|
||||
FileUtil.saveFile(file, session.getServletContext().getRealPath("images"));
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
/***
|
||||
* 多文件上传方式2 分别为不同文件指定不同名字
|
||||
*/
|
||||
@PostMapping("upFiles2")
|
||||
public String upFile(String extendParam,
|
||||
@RequestParam(name = "file1") MultipartFile file1,
|
||||
@RequestParam(name = "file2") MultipartFile file2, HttpSession session) {
|
||||
String realPath = session.getServletContext().getRealPath("images2");
|
||||
FileUtil.saveFile(file1, realPath);
|
||||
FileUtil.saveFile(file2, realPath);
|
||||
System.out.println("extendParam:" + extendParam);
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("upFileForDownload")
|
||||
public String upFileForDownload(MultipartFile file, HttpSession session, Model model) throws UnsupportedEncodingException {
|
||||
String path = FileUtil.saveFile(file, session.getServletContext().getRealPath("/image"));
|
||||
model.addAttribute("filePath", URLEncoder.encode(path,"utf-8"));
|
||||
model.addAttribute("fileName", file.getOriginalFilename());
|
||||
return "fileDownload";
|
||||
}
|
||||
|
||||
/***
|
||||
* 下载文件
|
||||
*/
|
||||
@GetMapping("download")
|
||||
public ResponseEntity<byte[]> downloadFile(String filePath) throws IOException {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
File file = new File(filePath);
|
||||
// 解决文件名中文乱码
|
||||
String fileName=new String(file.getName().getBytes("UTF-8"),"iso-8859-1");
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
headers.setContentDispositionFormData("attachment", fileName);
|
||||
|
||||
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
|
||||
headers, HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
}
|
@ -15,13 +15,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping("hello")
|
||||
private String hello() {
|
||||
public String hello() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("auth")
|
||||
private void auth() {
|
||||
public void auth() {
|
||||
throw new NoAuthException("没有对应的访问权限!");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,56 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.datetime.DateFormatter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :参数绑定
|
||||
*/
|
||||
@Controller
|
||||
public class ParamBindController {
|
||||
|
||||
@InitBinder
|
||||
protected void initBinder(WebDataBinder binder) {
|
||||
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
|
||||
// 参数绑定与日期格式转换
|
||||
@RequestMapping("param")
|
||||
public String param(String name, int age, double salary, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birthday, Model model) {
|
||||
model.addAttribute("name", name);
|
||||
model.addAttribute("age", age);
|
||||
model.addAttribute("salary", salary);
|
||||
model.addAttribute("birthday", birthday);
|
||||
return "param";
|
||||
}
|
||||
|
||||
@RequestMapping("param2")
|
||||
public String param2(String name, int age, double salary, Date birthday, Model model) {
|
||||
model.addAttribute("name", name);
|
||||
model.addAttribute("age", age);
|
||||
model.addAttribute("salary", salary);
|
||||
model.addAttribute("birthday", birthday);
|
||||
return "param";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("param3")
|
||||
public String param3(Programmer programmer, String extendParam, Model model) {
|
||||
System.out.println("extendParam" + extendParam);
|
||||
model.addAttribute("p", programmer);
|
||||
return "param";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.datetime.DateFormatter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :数据校验
|
||||
*/
|
||||
@RestController
|
||||
public class ParamValidController {
|
||||
|
||||
|
||||
@PostMapping("validate")
|
||||
public void valid(@Validated Programmer programmer,
|
||||
BindingResult bindingResult) {
|
||||
List<ObjectError> allErrors = bindingResult.getAllErrors();
|
||||
for (ObjectError error : allErrors) {
|
||||
System.out.println(error.getDefaultMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.heibaiying.controller;
|
||||
|
||||
import com.heibaiying.bean.Pet;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Restful 风格的请求
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class RestfulController {
|
||||
|
||||
|
||||
@GetMapping("restful/owners/{ownerId}/pets/{petId}")
|
||||
public void get(@PathVariable String ownerId, @PathVariable String petId) {
|
||||
System.out.println("ownerId:" + ownerId);
|
||||
System.out.println("petId:" + petId);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("restful2/owners/{ownerId}/pets/{petId}")
|
||||
public void get(@ModelAttribute Pet pet) {
|
||||
System.out.println("ownerId:" + pet.getOwnerId());
|
||||
System.out.println("petId:" + pet.getPetId());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.heibaiying.convert;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
public class CustomDateConverter implements Converter<String, Date> {
|
||||
|
||||
public Date convert(String s) {
|
||||
try {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return simpleDateFormat.parse(s);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.heibaiying.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 文件上传工具类
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
public static String saveFile(MultipartFile file, String path) {
|
||||
String fullPath = path + File.separator + file.getOriginalFilename();
|
||||
try {
|
||||
File saveDir = new File(path);
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
FileOutputStream outputStream = new FileOutputStream(new File(fullPath));
|
||||
InputStream inputStream = file.getInputStream();
|
||||
byte[] bytes = new byte[1024 * 1024];
|
||||
int read;
|
||||
while ((read = inputStream.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, read);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
}
|
@ -13,8 +13,8 @@
|
||||
<!--使用默认的Servlet来响应静态文件 详见README.md -->
|
||||
<mvc:default-servlet-handler/>
|
||||
|
||||
<!-- 开启注解驱动 详见 README.md -->
|
||||
<mvc:annotation-driven/>
|
||||
<!-- 开启注解驱动 并制定全局日期转换 详见 README.md -->
|
||||
<mvc:annotation-driven conversion-service="formattingConversionService"/>
|
||||
|
||||
<!-- 配置视图解析器 -->
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
|
||||
@ -41,4 +41,20 @@
|
||||
<!--配置全局异常处理器-->
|
||||
<bean class="com.heibaiying.exception.NoAuthExceptionResolver"/>
|
||||
|
||||
<!-- 全局日期格式转换 -->
|
||||
<bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
|
||||
<property name="converters">
|
||||
<list>
|
||||
<bean class="com.heibaiying.convert.CustomDateConverter"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!--配置文件上传-->
|
||||
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
|
||||
<property name="maxUploadSize" value="102400000"/>
|
||||
<property name="maxUploadSizePerFile" value="10240000"/>
|
||||
<property name="defaultEncoding" value="utf-8"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
32
spring/springmvc-base/src/main/webapp/WEB-INF/jsp/file.jsp
Normal file
32
spring/springmvc-base/src/main/webapp/WEB-INF/jsp/file.jsp
Normal file
@ -0,0 +1,32 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>文件上传</title>
|
||||
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/file.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFile" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件:<input name="file" type="file"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFiles" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件(多选):<input name="file" type="file" multiple><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFiles2" method="post" enctype="multipart/form-data">
|
||||
请选择上传文件1:<input name="file1" type="file"><br>
|
||||
请选择上传文件2:<input name="file2" type="file"><br>
|
||||
文件内容额外备注: <input name="extendParam" type="text"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
<form action="${pageContext.request.contextPath }/upFileForDownload" method="post" enctype="multipart/form-data">
|
||||
上传并下载:<input name="file" type="file"><br>
|
||||
<input type="submit" value="点击上传文件">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>文件下载</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="${pageContext.request.contextPath}/download?filePath=${filePath}">${fileName}</a>
|
||||
</body>
|
||||
</html>
|
15
spring/springmvc-base/src/main/webapp/WEB-INF/jsp/param.jsp
Normal file
15
spring/springmvc-base/src/main/webapp/WEB-INF/jsp/param.jsp
Normal file
@ -0,0 +1,15 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Restful</title>
|
||||
<script src="${pageContext.request.contextPath}/js/jquery3.3.1.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
<li>姓名:${empty name ? p.name : name}</li>
|
||||
<li>年龄:${empty age ? p.age : age}</li>
|
||||
<li>薪酬:${empty salary ? p.salary : salary}</li>
|
||||
<li>生日:${empty birthday ? p.birthday : birthday}</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
操作成功!
|
||||
</body>
|
||||
</html>
|
3
spring/springmvc-base/src/main/webapp/css/file.css
Normal file
3
spring/springmvc-base/src/main/webapp/css/file.css
Normal file
@ -0,0 +1,3 @@
|
||||
form {
|
||||
border: 1px green dashed;
|
||||
}
|
10364
spring/springmvc-base/src/main/webapp/js/jquery3.3.1.js
Normal file
10364
spring/springmvc-base/src/main/webapp/js/jquery3.3.1.js
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user