SSM实现图⽚上传很多新⼿都头疼于SSM上传图⽚如何实现,今天就来做个最简单的例⼦进⾏展⽰开发⼯具以及环境:eclipse、navicat、mysql、tomcat7
⼀、数据库的设计
在navicat新建⼀个⽤户表people,字段分别是id、姓名、性别、⼿机、地址、图⽚
以下是我的⽬录结构
⼆、SSM的配置
1)引⼊SSM常⽤jar包,百度⼀下⽹上蛮多的
2)l
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xsi:schemaLocation="java.sun/xml/ns/j  <display-name&batis</display-name>
<!-- 配置SpringMVC编码过滤器 -->
<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>
<!-- 启动SpringMVC -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 参数:读取l -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 启动spring -->
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<!-- 修改路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3)l
xmlns:mvc="/schema/mvc"
xmlns:contenxt="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd
/schema/context
/schema/context/spring-context.xsd">
<!-- 扫描Controller所在的包 -->
<contenxt:component-scan base-package="ller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:简化在Controller类编写的视图路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- ⽂件上传的解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipartmons.CommonsMultipartResolver">        <!-- 上传图⽚的最⼤尺⼨ 10M-->
<property name="maxUploadSize" value="10485760"></property>
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
4)l
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/tx/spring-tx.xsd">
<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 创建DataSource -->
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- 创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载sql映射⽂件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- Mapper接⼝的扫描 -->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接⼝所在包路径  -->
<property name="basePackage" value="com.wt.dao"/>
</bean>
<!-- 开启Spring的IOC注解扫描 -->
<context:component-scan base-package="com.wt"/>
<!-- 开启Spring的事务 -->
<!-- -事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 启⽤Spring事务注解 -->spring ioc注解
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
5)jdbc.properties
//你的数据库名和密码配置
jdbc.url=jdbc:mysql://127.0.0.1/people
jdbc.sql.jdbc.Driver
jdbc.user=root
jdbc.password=1234
6)log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
三、复制粘贴代码
user.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。