springmvc图片上传及json数据转换过程详解
作者:骑猪南下L 时间:2022-02-25 17:11:14
springmvc的图片上传
1.导入相应的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属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 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数据转换
注解格式转换@ResponseBody
工具类JSONResult
package com.liuwenwu.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;
}
}
HelloController
@ResponseBody
@RequestMapping("/jsonData3")
public JSONResult jsonData3(){
return JSONResult.ok("成功:这里可以存放字符串、对象、数组、集合都行,这样可以节省拼接map结合的过程");
}
@ResponseBody
@RequestMapping("/jsonData4")
public JSONResult jsonData4(){
return JSONResult.errorMsg("失败:这里可以存放字符串、对象、数组、集合都行,这样可以节省拼接map结合的过程");
}
效果:
来源:https://www.cnblogs.com/liuwenwu9527/p/11637763.html
标签:spring,mvc,图片,上传,json,数据,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android实现简易计算器
2023-06-21 04:26:09
![](https://img.aspxhome.com/file/2023/4/83514_0s.jpg)
Android中通过RxJava进行响应式程序设计的入门指南
2023-06-27 08:17:46
Java 获取Web项目相对webapp地址的实例
2022-07-03 17:46:00
深入理解Java基础之try-with-resource语法糖
2022-12-03 08:17:43
浅谈spring security入门
2023-02-20 04:48:58
![](https://img.aspxhome.com/file/2023/2/63782_0s.png)
浅谈java项目与javaweb项目导入jar包的区别
2023-11-11 11:06:19
Java使用Redisson分布式锁实现原理
2023-12-10 11:07:30
![](https://img.aspxhome.com/file/2023/1/68141_0s.png)
Java访问WebService返回XML数据的方法
2023-11-10 21:23:09
Java实战之用springboot+netty实现简单的一对一聊天
2023-12-03 07:28:19
![](https://img.aspxhome.com/file/2023/0/61080_0s.png)
Java新API的时间格式化
2023-02-14 19:24:37
![](https://img.aspxhome.com/file/2023/9/65319_0s.png)
java反射遍历实体类属性和类型,并赋值和获取值的简单方法
2023-07-15 04:37:18
实例解析Json反序列化之ObjectMapper(自定义实现反序列化方法)
2023-11-23 18:07:25
IntelliJ IDEA 15款超级牛逼插件推荐(自用,超级牛逼)
2023-10-10 05:51:22
![](https://img.aspxhome.com/file/2023/6/71166_0s.png)
springMVC利用FastJson接口返回json数据相关配置详解
2023-02-24 10:51:58
详解java中String、StringBuilder、StringBuffer的区别
2023-06-17 06:03:23
java实现人员信息管理系统
2023-11-02 05:21:31
![](https://img.aspxhome.com/file/2023/5/58425_0s.jpg)
Java三种IO模型原理实例详解
2022-05-20 02:36:37
Java try catch finally异常处理组合详解
2021-07-04 18:19:51
SpringMVC结构简介及常用注解汇总
2023-10-25 09:16:59
详解Java的Spring框架下bean的自动装载方式
2022-10-23 08:13:59
![](https://img.aspxhome.com/file/2023/2/64432_0s.png)