SpringMvc文件上傳和json處理


文件上傳

pom依賴

 

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

 

springmvc.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>

jsp頁面

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2019/9/29
  Time: 19:57
  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="xxx">
    <input type="submit" value="上傳">
</form>

</body>
</html>

代碼

    @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("請求異常");

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM