CREATE TABLE `t_images` (
`imgid` int(11) NOT NULL AUTO_INCREMENT,
`storepath` varchar(255) DEFAULT NULL,
`storename` varchar(255) DEFAULT NULL,
`intro` varchar(255) DEFAULT NULL,
`isenabled` bit(1) DEFAULT NULL,
`inputdate` datetime DEFAULT NULL,
PRIMARY KEY (`imgid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
//图片的id(编号)
private Integer imgid;
//图片的路径
private String storepath;
//图片的名称
private String storename;
//图片的介绍(描述)
private String intro;
//是否启用 true:启用 false:禁用
private Boolean isenabled;
//录入时间
private Date inputdate = new Date();
//上传的图片文件(和数据库没有关系,我们只是通过这个字段接收文件)
private MultipartFile fileImg;
public interface IImagesDao {
void save(Images images);
void update(Images images);
void delete(Integer id);
Images findOne(Integer id);
List<Images> findAll();
}
public interface IImageService {
void save(Images images);
void update(Images images);
void delete(Integer id);
Images findOne(Integer id);
List<Images> findAll();
}
package cms.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import cms.domain.Images;
import cms.service.IImageService;
@Controller
@RequestMapping("/img")
public class ImagesController {
@Autowired
private IImageService imgservice;
@RequestMapping("/save")
public String save(Images images,HttpServletRequest req) throws IllegalStateException, IOException{
//一、解决上传的名称问题
//1.拿到对应的文件
MultipartFile fileImg = images.getFileImg();
//2.拿到文件名称
String filename = fileImg.getOriginalFilename();
//3.拿到文件后缀
String extension = FilenameUtils.getExtension(filename);
//4.获取随机名称
String uuid = UUID.randomUUID().toString();
//5.拼接新的文件名
String newfilename = uuid+"."+extension;
//二、解决上传的路径
//1.获取实际的路径
String realPath = req.getServletContext().getRealPath("/upload");
//2.创建文件
File file = new File(realPath,newfilename);
//3.创建文件夹
File parentFile = file.getParentFile();
if (!parentFile.exists()) {//如果不存在父文件夹
//创建文件夹
parentFile.mkdirs();
}
//三、保存文件 transferTo:保存文件的方法
fileImg.transferTo(file);
//添加数据
//1.添加名称
images.setStorename(filename);
//2.添加地址
images.setStorepath("/upload/"+newfilename);
imgservice.save(images);
return "forward:main";
}
//主页面
@RequestMapping("/index")
public String query(){
return "index";
}
//数据查询
@RequestMapping("/main")
public String tomain(Model model){
List<Images> findAll = imgservice.findAll();
model.addAttribute("imageList",findAll);
return "main";
}
@RequestMapping("/update")
public String update(){
return "main_add";
}
@RequestMapping("/delete")
public String delete(Integer imgid){
imgservice.delete(imgid);
return "forward:main";
}
@RequestMapping("/input")
public String input(){
return "main_add";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>cms</display-name>
<!-- Spring的核心配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- SpringMVC的配置文件的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--记SpringMVC跟着服务器(tomcat)的启动而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--使用杠更加符合咱们的RESTful风格 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 监听服务器(tomcat)的启动,让Spring也同启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置相应的过滤器:角色SpringMVC 的POST请求的乱码问题 -->
<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
" >
<!-- 打描包:支持对应的注解,变成bean -->
<context:component-scan base-package="cms.service,cms.dao" />
<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置dataSource(连接池) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
专门为咱们准备了一个类,用来完成数据库的操作:JdbcTemplate
JdbcTemplate:jDBC的模板(公共的代码已经完成)
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
" >
<!-- 引入其它的Spring配置文件 -->
<!-- <import resource="classpath:applicationContext.xml"/> -->
<!-- 扫描包 -->
<context:component-scan base-package="cms.controller" />
<!--支持SpringMVC特有的注解 -->
<mvc:annotation-driven />
<!-- 对静态资源放行 -->
<mvc:default-servlet-handler />
<!-- 视图解析器:自动为咱们添加前缀与后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>2000000000</value>
</property>
</bean>
</beans>
3.页面(WEN-INF安全目录必须通过controller进入)
4.和前端进行数据交互
5.文件上传