SpringMvc文件上传和json处理

2020-12-13 15:44

阅读:539

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:print   for   用户   pre   rop   stack   bsp   返回   art   

文件上传

pom依赖

 

commons-fileupload
      commons-fileupload
      1.3.3

 

springmvc.xml

"multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        "defaultEncoding" value="UTF-8">"maxUploadSize" value="52428800">"resolveLazily" value="true"/>

jsp页面

  Created by IntelliJ IDEA.
  User: dell
  Date: 2019/9/29
  Time: 19:57
  To change this template use File | Settings | File Templates.
--%>
"text/html;charset=UTF-8" language="java" %>上传图片
"${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data"> "file" name="xxx"> "submit" value="上传">

代码

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(HttpServletRequest request, MultipartFile xxx){
        try {
            FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File("E://temp/"+xxx.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传成功";
    }

如果要访问这张图片要配置映射文件:

技术图片

 

结果:

技术图片

 

json返回处理:

在请求方法前加这个@ResponseBody
  @ResponseBody
    @RequestMapping("/jsonStr2")
    public Map jsonStr2(){
        return bookService.listPager(new Book(),new PageBean()).get(0);

    }

结果

技术图片

json返回处理的工具类

package com.cjh.util;

public class JSONResult {

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;
    
    private String ok;    // 不使用

    public static JSONResult build(Integer status, String msg, Object data) {
        return new JSONResult(status, msg, data);
    }

    public static JSONResult ok(Object data) {
        return 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("/jsonStr3")
    public JSONResult jsonStr3(){
        return JSONResult.ok("这里是data对象");

    }


    @ResponseBody
    @RequestMapping("/jsonStr4")
    public JSONResult jsonStr4(){
        return JSONResult.errorMsg("请求异常");

    }
}

技术图片

技术图片

 

SpringMvc文件上传和json处理

标签:print   for   用户   pre   rop   stack   bsp   返回   art   

原文地址:https://www.cnblogs.com/chenjiahao9527/p/11614170.html


评论


亲,登录后才可以留言!