SpringMVC 文件上傳 和 JSON處理

文件上傳

pom.xml

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

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>

upload.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/29 0029
  Time: 20:00
  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" id="">
    <button type="submit">提交</button>
</form>
</body>
</html>

文件上傳請求方法 HelloController

@RequestMapping("/upload")
public String upload(MultipartFile img){
    try {
        FileUtils.copyInputStreamToFile(img.getInputStream(),new File("D:/wpcache/"+img.getOriginalFilename()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "forward:hello2";
}

點擊上傳
在這裏插入圖片描述
在相對應的文件內我們看到已經上傳成功
在這裏插入圖片描述

JSON處理

HelloController

 @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);
  }

jsonData1
在這裏插入圖片描述
jsonData2
在這裏插入圖片描述
JSON 返回結果處理工具類
JSONResult

package com.lst.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;
	}

}

方法

@RequestMapping("/ok")
    @ResponseBody
    public JSONResult ok(){
        return JSONResult.ok("成功");
    }


    @RequestMapping("/error")
    @ResponseBody
    public JSONResult error(){
        return JSONResult.errorMsg("失敗");
    }

效果展示
ok
在這裏插入圖片描述
error
在這裏插入圖片描述
over。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章