spring文件上传

时间:2023-12-19 12:03:08

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>