1.web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 配置加载spring数据源信息 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <!-- 配置spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置定时器监听器 --> <listener> <listener-class>com.lenovo.init.AccountListener</listener-class> </listener> <!-- 设置请求字符编码过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springMvc中央控制器 --> <servlet> <servlet-name>applicationContext</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <!-- 截取*.do所有请求,进行请求转向,业务分发 --> <servlet-mapping> <servlet-name>applicationContext</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--配置webservice.以services/结尾 --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <!-- 设置默认首页未index.jsp --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
2.applicationContext.xml(springMVC配置文件)
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"default-autowire="byName"><context:annotation-config /><context:component-scan base-package="com.lenovo" /> <!-- 自动扫描所有注解该路径 --><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 配置sqlMapClientFactory --><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation" value="classpath:config/sqlMapConfig.xml" /><property name="dataSource" ref="dataSource" /></bean><!-- 配置声明式事务 拦截器 --><bean id="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><property name="transactionManager" ref="transactionManager"></property><property name="transactionAttributes"><props><prop key="save*">PROPAGATION_REQUIRED</prop><prop key="delete*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="create*">PROPAGATION_REQUIRED</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><prop key="get*">PROPAGATION_REQUIRED,readOnly</prop><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean><!--aop 管理*.service管理 --><beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="proxyTargetClass" value="true"></property><property name="beanNames"><value>*Service</value></property><property name="interceptorNames"><list><value>transactionInterceptor</value></list></property></bean><!-- 配置parnetDao --><bean id="parentDao" class="com.lenovo.dao.impl.ParentDaoImpl"><property name="sqlMapClient" ref="sqlMapClient"></property></bean><!-- 配置parentService --><bean id="parentService" class="com.lenovo.service.impl.ParentServiceImpl"><property name="parentDao" ref="parentDao"></property></bean></beans>applicationContext-dataSource.xml(配置数据源)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><!-- 配置数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><!-- <property name="url"><value>jdbc:mysql://172.17.116.21:3306/lezhuomian?useUnicode=true&continueBatchOnError=true&characterEncoding=utf-8</value></property>--><property name="url"><value>jdbc:mysql://172.16.227.227:3306/lezhuomian?useUnicode=true&continueBatchOnError=true&characterEncoding=utf-8</value></property> <property name="username"><value>root</value></property><property name="password"><value></value></property><property name="removeAbandoned"><value>true</value></property><property name="defaultAutoCommit"><value>true</value></property><property name="removeAbandonedTimeout"><value>60</value></property><property name="validationQuery"><value>SELECT 1</value></property><property name="logAbandoned"><value>true</value></property><property name="testWhileIdle"><value>true</value></property><property name="timeBetweenEvictionRunsMillis"><value>10000</value></property><property name="minEvictableIdleTimeMillis"><value>6000</value></property><property name="testOnBorrow"><value>true</value></property><property name="maxActive"><value>150</value></property><property name="maxIdle"><value>30</value></property><property name="minIdle"><value>10</value></property><property name="initialSize"><value>10</value></property></bean></beans>applicationContext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:p="" xmlns:context="" xsi:schemaLocation=" "><context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="com.lenovo.servlet" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />--><!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> </bean> <!-- spring的异常处理,提供对用户比较友好的错误界面,可以指定不同的异常类型 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception"> errors/exception </prop> </props> </property> </bean> <!-- 设置响应字符编码 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /></beans>3.业务模型Controller
@Controller
@RequestMapping("file.do") public class FileUploadController {public FileUploadController() { logger.info("fileUpload init-----"); }
private ParentService parentService;
@Autowired
public void setParentService(ParentService parentService) { // logger.info("parentService==="+parentService); this.parentService = parentService;}
@RequestMapping(method = RequestMethod.POST, params = "method=upload1")
public String csfileUpload(HttpServletRequest request, ModelMap model) {MultipartHttpServletRequest muiltRequest = (MultipartHttpServletRequest) request;
String ztMc = request.getParameter("wjmc"); String wjMs = request.getParameter("wjms"); String filePath = "zt" + "/" + ztMc; // String fileRealPath = request.getSession().getServletContext() .getRealPath(filePath); // 服务器磁盘的地址 logger.info(fileRealPath); File file = new File(fileRealPath); if (!file.exists()) file.mkdirs(); MultipartFile multipartFile = muiltRequest.getFile("dc_file"); // 获得文件 String fileName = multipartFile.getOriginalFilename(); String path = fileRealPath + File.separator + fileName; File fl = new File(path);try {
multipartFile.transferTo(fl); FileToZip zip = new FileToZip(); zip.makeFile_Zip(fileRealPath + ".zip", path); //压缩文件 ZtWjSc ztwj = new ZtWjSc();parentService.insert("insertZtWjScs", ztwj); 插入数据库
} catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wjListAll(request, model);}
}
public class ParentDaoImpl extends SqlMapClientDaoSupport implements ParentDao {
public void insert(String poName, Object obj) {
getSqlMapClientTemplate().insert(poName, obj); System.out.println(poName + ".table,添加数据------>>");}
}
页面
<form id="signupForm" name="signupForm" method="post"
action="<%=request.getContextPath()%>/file.do" enctype="multipart/form-data" > <input type="hidden" name="method" value="upload1" /><span>主题中文名称:</span><input name="chname" type="text" size="20" id="reschname" class="resname" /><div id="error1" class="error1"><font color="red">中文名必填</font></div><div id="error11" class="error11"><font color="red">(1-5)个字符</font></div><br /><span>主题英文名称:</span><input name="engname" type="text" size="20" id="resenname" class="resname" /><div id="error2" class="error2"><font color="red">英文名必填</font></div><div id="error12" class="error12"><font color="red">(1-5)个字符</font></div><br /><span>版本号:</span> <input name="vername" type="text" size="20" id="resvername" class="resvername" /><div id="error3" class="error3"><font color="red">版本号必填</font></div><div id="error13" class="error13"><font color="red">(1-8)个字符</font></div><br /><span>资源包:</span> <input name="file" type="file" id="fileup" size="20" value="请上传需要打包的文件" /><div id="error4" class="error4"><font color="red">请上传需要打包的文件</font></div><div id="error5" class="error5"><font color="red">上传文件类型:zip,rar</font></div><br /><span>打包进度:</span></form>