Springmvc json file upload and processing

SpringMVC file upload

1. Relevant dependence

 

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
</dependency>

 

2. Configure springmvc-servlet.xml upload files parser

<bean the above mentioned id = " the MultipartResolver "  class = " org.springframework.web.multipart.commons.CommonsMultipartResolver " > 
        <-! and users must attribute the JSP pageEncoding agreement in order to resolve the correct form of content -> 
        <Property name = " defaultEncoding " value = " UTF-. 8 " > </ Property> 
        <-! maximum file size (bytes) 1024 * 1024 * 50 = 50M -> 
        <Property name = " maxUploadSize " value = " 52428800 " > </ property>
        <-! resolveLazily property is enabled to delay file parsing, file size in order to capture abnormal -> 
        <Property name = " resolveLazily " value = " to true " /> 
</ bean>

3.upload.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2019/9/29
  Time: 20:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
        上传文件:<input type="file" name="img" >
        <button type="submit">提交</button>
    </form>


</body>
</html>

HelloController:

@RequestMapping("/upload")
    public String upload(MultipartFile img) throws IOException {
        FileUtils.copyInputStreamToFile(img.getInputStream(),new File("D:/yyy/"+img.getOriginalFilename()));
        return "forward:upload";
    }

 

 

json processing

In the method of the above plus @ResponseBody like this comment

 

 

@ResponseBody
    @RequestMapping("/jsonData1")
    public List<Map> jsonData1(){
        return bookservice.listPager(new Book(),new PageBean());
    }

    @ResponseBody
    @RequestMapping("/jsonData2")
    public Map jsonData2(){
        return bookservice.listPager(new Book(),new PageBean()).get(0);
    }

 

 

 

Tools JSONResult:

com.psy.util Package; 

public  class JsonResult { 

    // response to a traffic state 
    Private Integer Status; 

    // response message 
    Private String MSG; 

    // data response 
    Private Object Data; 
    
    Private String OK;     // do not use 

    public  static JsonResult Build (Status Integer, String MSG, Object Data) {
         return  new new JsonResult (Status, MSG, Data); 
    } 

    public  static JsonResult OK (Object Data) {
         return  new new JsonResult (Data); 
    } 

    public  static JSONResult ok() {
        return new JSONResult(null);
    }
    
    public static JSONResult errorMsg(String msg) {
        return new JSONResult(500, msg, null);
    }
    
    public static JSONResult errorMap(Object data) {
        return new JSONResult(501, "error", data);
    }
    
    public static JSONResult errorTokenMsg(String msg) {
        return new JSONResult(502, msg, null);
    }
    
    public static JSONResult errorException(String msg) {
        return new JSONResult(555, msg, null);
    }

    public JSONResult() {

    }

    public JSONResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public JSONResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == 200;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }

}

 

@ResponseBody 
    @RequestMapping ( " / jsonData3 " )
     public JsonResult jsonData3 () { 
        the Map Map = new new the HashMap (); 

        return JSONResult.ok ( " Success: Here can store strings, objects, arrays, collections do, it saves splicing process map set " ); 
    } 

    @ResponseBody 
    @RequestMapping ( " / jsonData4 " )
     public JsonResult jsonData4 () {
         return JSONResult.errorMsg ( " failed: this can have strings, objects, arrays, collections do, it saves splicing process map set " ); 
    }

 

 

 

Guess you like

Origin www.cnblogs.com/psyu/p/11610234.html