文档章节

SpringMvc+ibaits 文件上传的列子

wsl_Mr
 wsl_Mr
发布于 2013/03/06 10:42
字数 1332
阅读 379
收藏 1

码上生花,ECharts 作品展示赛正式启动!>>>

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管理 -->
<bean
class="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&amp;continueBatchOnError=true&amp;characterEncoding=utf-8</value>
</property>
-->

<property name="url">
<value>jdbc:mysql://172.16.227.227:3306/lezhuomian?useUnicode=true&amp;continueBatchOnError=true&amp;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="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;<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>

 

 

 

© 著作权归作者所有

wsl_Mr
粉丝 9
博文 70
码字总数 26504
作品 0
海淀
私信 提问
加载中
此博客有 3 条评论,请先登录后再查看。
有没有基于springmvc+ibaits + Hadoop +hbase 的大数据架构的项目例子,供我们码农学习和了解

有没有基于springmvc+ibaits + Hadoop +hbase 的大数据架构的项目例子,供我们码农学习和了解

天池番薯
2015/10/09
4.2K
10
容器Docker学习系列六~命令学习仓库命令

在前面几篇文章中,我们学习了怎么安装Docker,并进行本地Docker的操作,现在我们就学习下Docker仓库的学习,其实Docker很多理念操作也是来自git,比如我们在下载一个镜像的时候,可以看到多个...

LuckQI
2018/08/21
0
0
30行java代码 实现网络视频m3u8文件完整上传oss(实现可播放)

##30行java代码 实现网络视频m3u8文件完整上传oss(可播放) java高并发入门篇 m3u8文件简介: m3u8参数(**不看也可**) m3u8文件打开图 index.m3u8文件上传oss代码 **注意:m3u8文件需和ts文...

易等于亿
04/13
0
0
layui文件上传进度条(模拟)

1.修改上传组件js(没测) https://blog.csdn.net/weixin_42457316/article/details/81017471 https://www.cnblogs.com/youmingkuang/p/9183528.html https://fly.layui.com/jie/19430/ 1、upl......

osc_tnf99tdd
2018/08/22
6
0
宝塔 windows下apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件

首先我们来看两段对上传目录设置无权限的列子,配置如下: //在宝塔下如用/upload这个路径应用无效,一定要C:/wwwroot/upload才有效果 <Directory "要去掉PHP执行权限的目录路径,如/upload...

osc_cesboqi4
2019/08/21
2
0

没有更多内容

加载失败,请刷新页面

加载更多

聊聊puma的ChangedEvent

序 本文主要研究一下puma的ChangedEvent Event puma/core/src/main/java/com/dianping/puma/core/event/Event.java public abstract class Event implements Serializable {private stat......

go4it
26分钟前
9
0
为什么Kubernetes和容器与机器学习密不可分?

当前,数字化转型的热潮在IT领域发展的如火如荼,越来越多的企业投身其中,机器学习和人工智能等现代技术的融合在公司组织内部也逐渐流行起来。 随着那些构成企业复杂IT基础架构的技术日益成...

京东智联云开发者
27分钟前
5
0
协程swoole对比golang

协程概念 协程(Coroutines)是一种比线程更加轻量级的存在,正如一个进程可以拥有多个线程一样,一个线程可以拥有多个协程。 协程具有以下几个特点 用户态执行,完全由程序所控制,不是被操...

冻结not
44分钟前
5
0
如何在macOS或OS X上安装pip? - How do I install pip on macOS or OS X?

问题: This post is a Community Wiki . 这篇文章是社区维基 。 Edit existing answers to improve this post. 编辑现有答案以改善此职位。 It is not currently accepting new answers. 它......

fyin1314
今天
11
0
net.sf.JSONException Map中value字段为null,服务响应500

6月2号凌晨系统生产发布,2号白天仓库作业时有仓库拣选单打印面单接口500,不是所有的拣选单掉接口都是500, 当时通过接口响应正产和响应500,逻辑梳理以及数据对比,最后发现是Map 对象里面...

Lbj虞
今天
13
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部