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文件上传的更多相关文章

  1. Spring文件上传出错:java.lang.ClassCastException: org.apache.catalina.connector.Request

    java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.spri ...

  2. Spring 文件上传MultipartFile 执行流程分析

    在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...

  3. Spring文件上传配置

    增加依赖jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...

  4. Spring 文件上传功能

    本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...

  5. (转)Spring文件上传,包括一次选中多个文件

    背景: http://www.cnblogs.com/lixuwu/p/8495275.html已经实现了单文件的上传和下载,多文件的上传是另一种情景,这里记录下来 实现过程 先说前台. 运行以后就是 ...

  6. Spring文件上传Demo

    package com.smbea.controller; import java.io.File; import java.io.FileOutputStream; import java.io.I ...

  7. spring mvc文件上传,request对象转换异常

    spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...

  8. Spring CommonsMultipartResolver上传文件小结

    自从业至今,文件上传与IO流之类的调用,一直是理解比较模糊的地方,大多就这网上搜到的资料抄抄改改草草了事,内部原理一直不甚了解,今日我们通过Spring的CommonsMultipartResolve ...

  9. Java Web文件上传

    参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...

随机推荐

  1. 扩展entity framework core 实现默认字符串长度,decimal精度,entity自动注册和配置

    报道越短,事情越严重!文章越短,内容越精悍! 文章以efcore 2.0.0-preview2.测试验证通过.其他版本不保证使用,但是思路不会差太远.源代码 目标: 1.实现entity的自动发现和m ...

  2. 【PHP】linux+php5.1.6+mysql5.0.2+apache2.0.55安装配置说明(转)

    linux+php5.1.6+mysql5.0.2+apache2.0.55安装配置说明:一.mysql5.0.2的安装配置过程及说明:1. #tar -zvxf mysql-5.0.2-alpha. ...

  3. 1.Why Apache Spark?

    Why Apache Spark? 1 Why Apache Spark 2 关于Apache Spark 3 如何安装Apache Spark 4 Apache Spark的工作原理 5 spark ...

  4. python中list总结

    转自python中list总结 一.list可以看做是一个数据结构,也是一个class, 用help(list)可以看见其方法,元素的增删改查都有各种现成的方法, 二.list操作包含以下函数:1.c ...

  5. Scrapy爬取西刺代理ip流程

    西刺代理爬虫 1. 新建项目和爬虫 scrapy startproject daili_ips ...... cd daili_ips/ #爬虫名称和domains scrapy genspider ...

  6. C++ STL 优先队列详解

    一.解释: 优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序,每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储. 例如,将元 ...

  7. AVR开发 Arduino方法(一) 端口子系统

    Arduino UNO R3使用的主处理器ATMega328P上有3个8位的输入/输出端口,它们分别是PB,PC和PD.Arduino IDE提供的Blink示例可以帮助我们了解端口的数字输出功能: ...

  8. nodejs-基础

    01-nodejs介绍 1.什么是nodejs 1.(javascript跑在机器端,服务端)Javascript on the machine 2.(跑在谷歌v8引擎上)A runtime for ...

  9. Android融合推送MixPush SDK集成多家推送平台,共享系统级推送,杀死APP也能收到推送

    消息推送是App运营的重要一环,为了优化消息推送成功率,降低电量和流量消耗,系统级的推送服务显得尤为重要.小米和魅族由此推出了自家的推送平台,在MIUI和Flyme上共享系统级推送服务,让APP在被杀 ...

  10. table表格中单击添加动态编辑框

    var $newNode=$("<input type='text' style='width:250px; height:20px; maxlength='20' id='texti ...