• Spring Mvc 上传文件Demo 实例


    返得利购物。 淘宝。京东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>


  • 相关阅读:
    切割自动贴标机功能、原理与常见问题回答
    WINCE中蓝牙扫描模块现10050,控制器无法找到错误的解决思路
    在WINCE中基于ActiveSync的Socket通信 c#
    cs关于房间分配
    JS判断单、多张图片加载完成
    在网站中添加 React
    tcp读取数据的方法
    微软发布2011年耐用消费品行业微软CRM解决方案 一方水土
    Microsoft Dynamics CRM4.0介绍 一方水土
    徐工集团再度携手瑞泰搭建Call Center管理平台 一方水土
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6801273.html
Copyright © 2020-2023  润新知