spring文件上传
Spring文件上传
1,导包:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!--文件上传所需jar包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
2,提交数据部分的index.html文件(用引入ueditor的方式)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" charset="utf-8" src="ueditor01/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="ueditor01/ueditor.all.min.js"> </script>
<!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="ueditor01/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
<!-- 表单设置次属性进行文件上传 -->
<form action="test/demo.do" onsubmit="getUserDetail()"
method="post" enctype="multipart/form-data">
编号:<input type="text" name="user_id"/>
名称:<input type="text" name="user_name"/>
密码:<input type="text" name="user_pwd"/>
添加时间:<input type="text"
name="user_addtime" value="2017-08-08"/>
<!-- 加一个隐藏框携带提交编辑器内容 -->
学生简介:<input type="hidden"
name="user_detail" id="user_detail"/>
<script id="myeditor" type="text/plain"
style="width:1024px;height:500px;"></script>
<script type="text/javascript">
//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('myeditor');
function getUserDetail(){
//表单提交前将编辑器的值取出赋值为隐藏框
var c=UE.getEditor('myeditor').getContent();
document
.getElementById("user_detail").value=c;
}
</script>
<input type="submit" value="提交"/>
</form>
</body>
</html>
3,spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.tarena" />
<!-- 配置MVC注解扫描 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
</beans>
4,实体类
package com.tarena.model;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class User {
private String user_id; //用户id
private String user_name; //用户姓名
private String user_pwd; //用户密码
//防止接受时间出错 接受时间格式 年-月-日
//@DateTimeFormat(pattern="yyyy-MM-dd")
private Date user_addtime; //用户添加时间
private String user_detail; //用户详细描述
public User() {
super();
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_pwd() {
return user_pwd;
}
public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}
public Date getUser_addtime() {
return user_addtime;
}
public void setUser_addtime(Date user_addtime) {
this.user_addtime = user_addtime;
}
public String getUser_detail() {
return user_detail;
}
public void setUser_detail(String user_detail) {
this.user_detail = user_detail;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_pwd=" + user_pwd + ", user_addtime="
+ user_addtime + ", user_detail=" + user_detail + "]";
}
}
5,时间格式转换类(用于让controller类继承,实现时间格式化的作用)
package com.tarena.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
public abstract class BaseController {
// 参数绑定转换器 在方法被执行之前会执行次绑定
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}
6,controller类(用model对象绑定从表单获取的所有对象,并显示在另一个jsp文件上)
package com.tarena.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.tarena.model.User;
@Controller
@RequestMapping("/test")
// 父路径 该类中所有访问路径都需要加上前缀
public class TestController extends BaseController {
// RequestMethod.POST 只允许post方式进行访问
// RequestMethod.GET 只允许get方式进行访问
// 直接输入网址进行访问属于 get请求
@RequestMapping(value = "/demo.do", method = RequestMethod.POST)
public String save(User user, Model m) {
System.out.println(user);
m.addAttribute("user", user);
return "demo";
}
}
7,显示对象的jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${user}</h1>
</body>
</html>
spring文件上传的更多相关文章
- Spring文件上传出错:java.lang.ClassCastException: org.apache.catalina.connector.Request
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.spri ...
- Spring 文件上传MultipartFile 执行流程分析
在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...
- Spring文件上传配置
增加依赖jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...
- Spring 文件上传功能
本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...
- (转)Spring文件上传,包括一次选中多个文件
背景: http://www.cnblogs.com/lixuwu/p/8495275.html已经实现了单文件的上传和下载,多文件的上传是另一种情景,这里记录下来 实现过程 先说前台. 运行以后就是 ...
- Spring文件上传Demo
package com.smbea.controller; import java.io.File; import java.io.FileOutputStream; import java.io.I ...
- spring mvc文件上传,request对象转换异常
spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...
- Spring CommonsMultipartResolver上传文件小结
自从业至今,文件上传与IO流之类的调用,一直是理解比较模糊的地方,大多就这网上搜到的资料抄抄改改草草了事,内部原理一直不甚了解,今日我们通过Spring的CommonsMultipartResolve ...
- Java Web文件上传
参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...
随机推荐
- guava缓存底层实现
摘要 guava的缓存相信很多人都有用到, Cache<String, String> cache = CacheBuilder.newBuilder() .expireAfterWrit ...
- oracle高级查询(实例基于scott用户四张表)
oracle高级查询(实例基于scott用户四张表) 分组查询 多表查询 子查询 综合实例 ====================================================== ...
- 关于EasyUI中DataGrid控件的一些使用方法总结
一,DataGrid 控件的工作流程 1,通过JavaScript将一个空白的div美化成一个空白的Datagrid模板 2,Datagrid模板通过制定的Url发送请求,获取数据 ...
- Java设计模式之模板方法设计模式(银行计息案例)
不知道为什么,这几天对Java中的设计模式非常感兴趣,恰巧呢这几天公司的开发任务还不算太多,趁着有时间昨天又把模板方法模式深入学习了一下,做了一个客户在不同银行计息的小案例,感触颇深,今天给各位分享一 ...
- (转)HTTP 协议详解(基础)
HTTP 协议详解 作者: 小坦克 来源: 博客园 发布时间: 2012-02-14 13:32 阅读: 95523 次 推荐: 99 原文链接 [收藏] 相关文章:HTTP 协议 ...
- 【MYSQL】SQL 的join 区别
----------------------INNER JOIN--------------------------- 1. 三表联合查询 select XX,XX from a , b , c ...
- MySQL学习笔记(三):常用函数
一:字符串函数 需要注意的几个细节: 1.cancat中有一个字符串为null,则结果为null. 2.left(str,x) 和 right(str,x)中x为null,则不返回任何字符串,不是nu ...
- helpers.bulk时 action_request_validation_exception 异常
语言Python 在开发时,批量插入ES,出现了action_request_validation_exception异常.我的代码是这样的 action = { } helpers.bulk(es, ...
- Scrapy提取多个标签的text
对于要提取嵌套标签所有内容的情况, 使用string或//text(), 注意两者区别 >>> from scrapy import Selector >>> &g ...
- 调皮的QQ音乐API:修复无法获取歌单
上一篇完整版:http://www.cnblogs.com/TwilightLemon/p/7076938.html QQ音乐的API真是太调皮了,获取歌单的API又更换了好多次,喵喵喵 旧版API( ...