1. 二进制存在数据库
  2. 存储到具体的硬盘目录
  3. 存储到服务器中
    真实路径与虚拟路径
    通过虚拟路径获取真实路径
    request.getservletContext.getrealPath(虚拟路径);
    fileutil.copyInputStreamtofile(file.getInput);
    fileutil.copyofile(file,new file(真是路径))
  4. ①添加文件上传相关依赖

      <commons-fileupload.version>1.3.3</commons-fileupload.version>
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>${commons-fileupload.version}</version>
        </dependency>
    

    表单提交方式为method=“post” enctype="multipart/form-data"
    首先创建upload.jsp页面搭建一个上传文件的多功能表单

    Created by IntelliJ IDEA. User: myy Date: 2019/11/24 Time: 15:59 To change this template use File | Settings | File Templates. <%@ page contentType="text/html;charset=UTF-8" language="java" %> <title>文件上传</title> </head> <form action="${pageContext.request.contextPath}/book/upload" method="post" enctype="multipart/form-data"> 待上传文件:<input type="file" name="file"> <input type="submit"> </form> </body> </html>

    完成图片的虚拟映射
    springmvc文件上传和对json的支持_spring技巧
    springmvc文件上传和对json的支持_Spring_02
    springmvc文件上传和对json的支持_spring技巧_03
    文件上传不能用简单的本地资源解析器做处理,要多功能解析器
    到springmvc-servlet.xml加入以下:

    <!-- 多功能解析器 -->
     <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>
     

    注意:记得修改
    springmvc文件上传和对json的支持_spring教程_04

    运行:
    选择你要上传的文件,提交
    springmvc文件上传和对json的支持_spring教程_05
    提交成功后:
    springmvc文件上传和对json的支持_spring教程_06
    图片的虚拟映射查看:
    springmvc文件上传和对json的支持_Spring_07

    对json的支持
    package com.myy.ssm.controller;
    import com.myy.ssm.model.Book;
    import com.myy.ssm.service.BookService;
    import com.myy.ssm.utils.PageBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.HttpServletRequest;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     * @author 熊贝贝
     * @site www.myy.com
     * @company
     * @create  2019-11-23 14:28
     * springmvc中五种返回值处理情况
     * 转发三种
     *  转发到安全目录web-inf下
     *  转发到根目录
     *  转发到requestMapping
     * 重定向两种
     *  根目录
     *  requestMapping
    @Controller
    public class HelloController {
        @Autowired
        private BookService bookService;
        @RequestMapping("/say1")
        public String say1(){
            return "hello";
        @RequestMapping("/say2")
        public ModelAndView say2(){
            ModelAndView mv = new ModelAndView();
            mv.setViewName("/hello2");
            mv.addObject("msg","换种方式继续");
            return mv;
        //转发到安全目录web-inf下
        @RequestMapping("/req1")
        public String req1() {
            System.out.println("转发到安全目录web-inf下...");
            return "a";
        //转发到根目录
        @RequestMapping("/req2")
        public String req2() {
            System.out.println("转发到根目录...");
            return "forward:/b.jsp";
        //转发到requestMapping
        @RequestMapping("/req3")
        public String req3() {
            System.out.println("转发到requestMapping...");
            return "forward:/req2";
        //根目录
        @RequestMapping("/red1")
        public String red1() {
            System.out.println("red1...");
            return "redirect:/b.jsp";
        //requestMapping
        @RequestMapping("/red2")
        public String red2() {
            System.out.println("red2...");
            return "redirect:/req2";
        @ResponseBody
        @RequestMapping("/json1")
        public Map json1(HttpServletRequest req){
    //        ObjectMapper om = new ObjectMapper();
    //        String s = om.writeValueAsString("这里既可以放map,也可以放list,也可放字符串");
            PageBean pageBean = new PageBean();
            pageBean.setRequest(req);
            List<Map> list = bookService.listPager(new Book(), pageBean);
            Map map = new HashMap();
            map.put("total",101);
            map.put("data",list);
            return map;
        @ResponseBody
        @RequestMapping("/json2")
        public List<Map> json2(HttpServletRequest req){
    //        ObjectMapper om = new ObjectMapper();
    //        String s = om.writeValueAsString("这里既可以放map,也可以放list,也可放字符串");
            PageBean pageBean = new PageBean();
            pageBean.setRequest(req);
            List<Map> list = bookService.listPager(new Book(), pageBean);
            return list;
        @ResponseBody
        @RequestMapping("/json3")
        public String json3(HttpServletRequest req){
            return "springmvc string to json";
     

    @ResponseBody 将结果进行json转换

    分别运行显示:
    json2:
    springmvc文件上传和对json的支持_spring教程_08
    json2:
    springmvc文件上传和对json的支持_spring技巧_09
    json格式:

    "bname": "斗破第13章", "price": 13.0, "bid": 13 }, { "bname": "圣墟第16章", "price": 16.0, "bid": 16 }, { "bname": "圣墟第18章", "price": 18.0, "bid": 18 }, { "bname": "圣墟第19章", "price": 19.0, "bid": 19 }, { "bname": "圣墟第20章", "price": 20.0, "bid": 20 }, { "bname": "圣墟第21章", "price": 21.0, "bid": 21 }, { "bname": "圣墟第22章", "price": 22.0, "bid": 22 }, { "bname": "仙逆第23章", "price": 23.0, "bid": 23 }, { "bname": "圣墟第24章", "price": 24.0, "bid": 24 }, { "bname": "斗破", "price": 123.0, "bid": 25

    json1:
    springmvc文件上传和对json的支持_Spring_10
    json格式:

    "total": 101, "data": [{ "bname": "斗破第13章", "price": 13.0, "bid": 13 }, { "bname": "圣墟第16章", "price": 16.0, "bid": 16 }, { "bname": "圣墟第18章", "price": 18.0, "bid": 18 }, { "bname": "圣墟第19章", "price": 19.0, "bid": 19 }, { "bname": "圣墟第20章", "price": 20.0, "bid": 20 }, { "bname": "圣墟第21章", "price": 21.0, "bid": 21 }, { "bname": "圣墟第22章", "price": 22.0, "bid": 22 }, { "bname": "仙逆第23章", "price": 23.0, "bid": 23 }, { "bname": "圣墟第24章", "price": 24.0, "bid": 24 }, { "bname": "斗破", "price": 123.0, "bid": 25

    json在线解析:https://www.sojson.com/