码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Mvc 上传文件Demo 实例

时间:2017-05-03 13:13:54      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:tmp   1.0   repo   location   out   except   param   ems   value   

返得利购物。 淘宝。京东500家商城合作,包括全面的商城返利网。注冊就送5元,购物就有返利。随时提现。

 同学们,新一轮的返利大潮正在慢慢靠近,让购物都认为自己在赚钱。购物,机票。游戏。酒店旅游,地方特色,娱乐,尽在www.bbuy8.com让你购物省钱,省心。【群号:335156195】

Controller 类

package com.upload.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/upload")
public class UpFileAction {
       @RequestMapping( "/uploadfile")
        public  String   upLoadFile(@RequestParam(value="upFile",required=false) MultipartFile upFile,HttpServletRequest request,Model model) throws IOException
        {
           String fileName = upFile.getOriginalFilename();  
           File targetFile = new File("D:/FILE_TMP",fileName);
           if(!targetFile.exists()){  
               targetFile.mkdirs();  
           }
           //開始保存文件
           upFile.transferTo(targetFile);
            return null;
        }
}


页面

<html>
<body>
<form action="upload/uploadfile"  method="post" enctype="multipart/form-data" accept-charset="utf-8">
<p>选择文件<input type="file"  name="upFile"><input type="submit" vlaue="=開始上传"></p>
</form>
</body>
</html>


web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
 
 
  <!-- 配置的Spring  标准 -->
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext.xml
        </param-value>
    </context-param>
    
      <!-- -设置Spring   监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <init-param>  
   <param-name>encoding</param-name>  
   <param-value>UTF-8</param-value>  
 </init-param>  
     
<!-- 配置核心类 -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:*-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <!-- 拦截请求 -->  
    <servlet-mapping>  
        <servlet-name>appServlet</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
</web-app>


applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"    default-autowire="byName">


    <!-- component-scan自己主动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
    
    <context:component-scan base-package="com.**.**" />

    <bean  id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
</beans>


appServlet-servlet.xml

<?

xml version="1.0" encoding="UTF-8"?

>
<!-- Bean头部 -->
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <!-- 激活@Controller模式 -->
    <mvc:annotation-driven />
    <!-- 对包中的全部类进行扫描,以完毕Bean创建和自己主动依赖注入的功能 须要更改 -->
   <context:component-scan base-package="com.**.**"></context:component-scan>

    <bean    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
   
   <mvc:default-servlet-handler />
    <mvc:resources location="/WEB-INF/index.jsp" mapping="/WEB-INF/index.jsp" />


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>

    </bean>
</beans>


Spring Mvc 上传文件Demo 实例

标签:tmp   1.0   repo   location   out   except   param   ems   value   

原文地址:http://www.cnblogs.com/lxjshuju/p/6801273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!