大体步骤:
测试
<person id="1">
<name>tomname>
<age>20age>
<salary>5000.0salary>
person>
(2)用JSON表示一个对象:{“id”:1,“name”:“tom”,“salary”:5000.0}
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
<script type="text/javascript">
// 定义json
var obj = {"id":123,"name":"王天霸","age":30};
// 弹出框
alert(obj.name);
alert(obj.age);
var arr = [{"name":"tom","age":20},{"name":"jack","age":30}];
alert(arr[1].age)
alert(arr[0].name)
script>
head>
<body>
body>
html>
会把数据转成json格式,并且不会走视图解析器了
@Controller
public class JsonController {
/**
* 返回一个json格式的user对象
* tips:406:缺少jar
*/
@RequestMapping("/user")
@ResponseBody // 会把数据转成json格式,并且不会走视图解析器了
public User json1(){
User user = new User(111, "力很弱", 20);
return user;
}
@RequestMapping("/list")
@ResponseBody // 会把数据转成json格式,并且不会走视图解析器了
public List<User> json2(){
User user1 = new User(111, "力很弱", 20);
User user2 = new User(222, "王天霸", 21);
User user3 = new User(333, "阮经天", 22);
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
}
User
public class User {
private Integer id;
private String name;
private Integer age;
@JsonFormat(pattern="yyyy^_^MM^_^dd HH^_^mm^_^ss",timezone="GMT+8")
private Date date;
public User() {
super();
}
public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User(Integer id, String name, Integer age, Date date) {
super();
this.id = id;
this.name = name;
this.age = age;
this.date = date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
JsonController
/**
* 日期格式问题
*/
@RequestMapping("/date")
@ResponseBody
public User json3(){
User user = new User(111, "力很弱", 20, new Date());
return user;
}
文件上传与下载实质都是IO流的读写操作
文件上传:把本地资源上传到服务器,即将本地磁盘中的文件复制到服务器指定的地方
输入流(读) - 输出流(写)
1.新建upload.html,填写上传表单
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
username: <input type="text" name="username"><br/>
图片:<input type="file" name="photo"><br/>
<input type="submit" value="上传">
form>
body>
html>
2.导包
commons-fileupload-1.2.2.jar,commons-io-1.4.jar
3.上传解析器
SpringMVC.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>#{1024*1024}value>
property>
<property name="maxUploadSize" value="1048576" />
bean>
以前写的username,password。。。 :简单的表单项
文件上传: 复杂表单项
tips:文件名和路径名尽量不要重复,如upload
UploadController
@Controller
public class UploadController {
/**
* photo: 复杂表单项---以前获取参数的方式不行了
* MultipartFile:文件项 接收的是一个地址值---包含了上传的内容了
*/
@RequestMapping("/upload")
public String uploadFile(MultipartFile photo){
boolean empty = photo.isEmpty();
if (!empty) {
String filename = photo.getOriginalFilename();
System.out.println(filename);
}
return null;
};
}
@Controller
public class UploadController {
/**
* photo: 复杂表单项---以前获取参数的方式不行了
* MultipartFile:文件项 接收的是一个地址值---包含了上传的内容了
*/
@RequestMapping("/upload")
public String uploadFile(MultipartFile photo,HttpServletRequest request) throws IOException{
if (!photo.isEmpty()) {
// 获取当前项目路径
String realPath = request.getServletContext().getRealPath("/upload1");
// 获取文件名 u=19008.jpg
String filename = photo.getOriginalFilename();
// 获取上传文件的后缀
int index = filename.lastIndexOf(".");
// 后缀
String suffix = filename.substring(index);
// 随机生成一个文件名 1610434039685.xml
// 获取时间戳
long i = System.currentTimeMillis();
// 拼接之后的名字
String name = i+suffix;
// 通过File构造获取一个完整的file路径
File file = new File(realPath, name);
// 输入流
InputStream input = photo.getInputStream();
// 获取输出流
FileOutputStream output = new FileOutputStream(file);
// 上传的核心代码
IOUtils.copy(input, output);
// 关流
output.close();
input.close();
}
// 重定向到当前页面
return "redirect:/uploadFile.html";
}
}
从服务器将文件复制到本地磁盘中
重点:输入流,输出流
把download目录放入工程中代表服务器资源
新建download.jsp
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<a href='/download?filename=<%=URLEncoder.encode("美女.jpg", "utf-8") %>'>美女.jpga><br/>
<a href="/download?filename=star.mp4">star.mp4a><br/>
<a href="/download?filename=suiji.zip">suiji.zipa><br/>
<a href="/download?filename=words.txt">words.txta><br/>
body>
html>
tips:要解决IE浏览器点击有中文的A标签报错400的问题,用uff-8编码解决
tips:
1.要解决浏览器能打开就直接打开不下载的问题
2.要解决中文乱码的问题(文件名乱码)
DownloadController
@Controller
public class DownloadController {
@RequestMapping("/download")
public void download(String filename,HttpServletRequest req,
HttpServletResponse resp) throws IOException{
// 获取download路径
String realPath = req.getServletContext().getRealPath("/download");
// 完整的file文件
File file = new File(realPath, filename);
// 不同的浏览器去处理中文乱码方式
if(req.getHeader("User-Agent").toUpperCase().indexOf("TRIDENT")!=-1){
filename = URLEncoder.encode(filename, "utf-8");
//电脑自带edge【edʒ】浏览器
}else if(req.getHeader("User-Agent").toUpperCase().indexOf("EDGE")!=-1){
filename = URLEncoder.encode(filename, "utf-8");
}else{//其他浏览器
filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1");//转码的方式
};
// 告诉浏览器我要以下载的方式处理这个文件
resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
// 获取输入流
FileInputStream input = new FileInputStream(file);
// 获取响应输出流
ServletOutputStream output = resp.getOutputStream();
// 核心代码
IOUtils.copy(input, output);
// 关流
output.close();
input.close();
}
}