文章目录
1、文件上传
- 添加文件上传相关依赖
<commons-fileupload.version>1.3.3</commons-fileupload.version>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
- 写一个文件上传请求方法
package com.hyf.ssm.controller;
import com.hyf.ssm.model.Book;
import com.hyf.ssm.service.BookService;
import com.hyf.ssm.utils.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.Map;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2019-11-23 1:13
*/
@Controller
@RequestMapping("/book") //指定命名空间 ...8080/boob/...
public class BookController {
@Autowired
private BookService bookService;
/* 文件上传 */
@RequestMapping("/upload")
public String upload(MultipartFile file) {
System.out.println(file.getOriginalFilename()); //上传的名字
System.out.println(file.getContentType());
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/图片放在这儿咯/" + file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/book/list";
}
}
- 在 spirngmvc-servlet.xml中配置文件上传解析器(CommonsMultipartResolver)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 1024*1024*50=50M-->
<property name="maxUploadSize" value="52428800"></property>
<!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<property name="resolveLazily" value="true"/>
</bean>
- uploa.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/book/upload" method="post" enctype="multipart/form-data">
上传文件:<<input type="file" name="file">
<button type="submit">上传</button>
</form>
</body>
</html>
-
访问上传此图片,需要完成图片的虚拟映射
-
测试
2、josn返回处理
在方法上面加入注解:@ResponseBody
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML
将查询出来的数据直接返回
package com.hyf.ssm.controller;
import com.hyf.ssm.model.Book;
import com.hyf.ssm.service.BookService;
import com.hyf.ssm.utils.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/book") //指定命名空间 ...8080/boob/...
public class BookController {
@Autowired
private BookService bookService;
/* josn */
@ResponseBody
@RequestMapping("/json1")
public Map json1(Book book,HttpServletRequest request){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List<Map> list = this.bookService.listPager(book, pageBean);
Map map = new HashMap();
map.put("total",pageBean.getTotal());
map.put("data",list);
return map;
}
@ResponseBody
@RequestMapping("/json2")
public List<Map> json2(Book book,HttpServletRequest request){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List<Map> list = this.bookService.listPager(book, pageBean);
return list;
}
@ResponseBody
@RequestMapping("/json3")
public String json3(Book book,HttpServletRequest request){
return "springmvc string to josn";
}
}