最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springmvc圖片上傳及json數(shù)據(jù)轉(zhuǎn)換過程詳解

 更新時(shí)間:2019年10月09日 09:56:12   作者:騎豬南下L  
這篇文章主要介紹了springmvc圖片上傳及json數(shù)據(jù)轉(zhuǎn)換過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

springmvc的圖片上傳

1.導(dǎo)入相應(yīng)的pom依賴

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

2.添加springmvc-servlet.xml里面的配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 必須和用戶JSP 的pageEncoding屬性一致,以便正確解析表單的內(nèi)容 -->
    <property name="defaultEncoding" value="UTF-8"></property>
    <!-- 文件最大大小(字節(jié)) 1024*1024*50=50M-->
    <property name="maxUploadSize" value="52428800"></property>
    <!--resolveLazily屬性啟用是為了推遲文件解析,以便捕獲文件大小異常-->
    <property name="resolveLazily" value="true"/>
  </bean>

3.前臺文件上傳表單

<%--
 Created by IntelliJ IDEA.
 User: ASUS
 Date: 2019/10/5
 Time: 10:25
 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

/**
   * 文件上傳
   * @param img
   * @return
   */
  @RequestMapping("/upload")
  public String upload(MultipartFile img){
    try {
      FileUtils.copyInputStreamToFile(img.getInputStream(),new File("F:/xxx/"+img.getOriginalFilename()));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "forward:hello3";
  }

json數(shù)據(jù)轉(zhuǎn)換

注解格式轉(zhuǎn)換@ResponseBody

工具類JSONResult

package com.liuwenwu.util;

public class JSONResult {

  // 響應(yīng)業(yè)務(wù)狀態(tài)
  private Integer status;

  // 響應(yīng)消息
  private String msg;

  // 響應(yīng)中的數(shù)據(jù)
  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;
  }

}

HelloController

@ResponseBody
  @RequestMapping("/jsonData3")
  public JSONResult jsonData3(){
    return JSONResult.ok("成功:這里可以存放字符串、對象、數(shù)組、集合都行,這樣可以節(jié)省拼接map結(jié)合的過程");
  }

  @ResponseBody
  @RequestMapping("/jsonData4")
  public JSONResult jsonData4(){
    return JSONResult.errorMsg("失?。哼@里可以存放字符串、對象、數(shù)組、集合都行,這樣可以節(jié)省拼接map結(jié)合的過程");
  }

效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

辽中县| 普兰店市| 嘉荫县| 蒲城县| 襄垣县| 常宁市| 固原市| 安图县| 临朐县| 杭锦后旗| 阜宁县| 万荣县| 香港| 莎车县| 汾西县| 广平县| 女性| 温泉县| 普洱| 揭阳市| 崇阳县| 华蓥市| 彭水| 乌兰察布市| 成安县| 千阳县| 上高县| 辽中县| 通山县| 合水县| 涪陵区| 高雄市| 赣榆县| 祁阳县| 阿尔山市| 永兴县| 犍为县| 图木舒克市| 莫力| 宁河县| 金山区|