返得利购物。 淘宝。京东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 实例的更多相关文章

  1. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

  2. Spring MVC 上传文件

    Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data"  input的typ ...

  3. Spring MVC上传文件原理和resolveLazily说明

    问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...

  4. 解析Spring MVC上传文件

    新建一个普通的maven工程 在pom.xml文件中引入相应的坐标 <?xml version="1.0" encoding="UTF-8"?> & ...

  5. spring MVC上传文件演示

    //相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...

  6. MVC:上传文件

    今天写了一个使用MVC上传的DEMO,很简单不超过10行代码.代码如下(关注重点,所以尽量精简掉其他代码): 项目结构

  7. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  8. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  9. Spring框架学习(8)spring mvc上传下载

    内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...

随机推荐

  1. 解决grep的结果无法显示文件名的问题

    有时候想在代码中执行某个关键词,会用下面的语句: find . -type f -name "*.java" | xargs grep -n "<keyword&g ...

  2. 【DB2】查询上月末、上年末、上年同期等信息

    此处以20180612为例子 想得到上年末.上年同期.上月末这些时间点,只需要记住函数ADD_MONTHS.LAST_DAY.ADD_YEARS这些函数即可. 上年末 SELECT SUBSTR(TO ...

  3. 【DB2】在使用EXISTS时,查询结果中包含聚合函数,一个不注意就会犯错的坑

    需求描述 现在需要通过EXISTS中的语句来控制查询结果是否存在 第一次实现SQL SELECT 1 AS ID,SUM(1) FROM (SELECT ID,NAME FROM (VALUES(1, ...

  4. Sqlldr导入txt文件内容到数据库中

    需求:数据迁移,将txt文件中的内容导入oracle数据库的表中,文本文件中数据格式如下(数据以空格隔开) 1. 创建与文本数据格式相匹配的表(此处在scott用户下创建) create table ...

  5. 【SpringMVC学习01】宏观上把握SpringMVC框架

    springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...

  6. R语言的if-else

    语法问题,类似于for函数使用时必须把"{"放在当前行,而不可以独占一行.R的特色. 具体你的这个问题可以尝试 x <- 5 if (x>0){ print(" ...

  7. Ooui:在浏览器中运行.NET应用 Ooui.Wasm:浏览器中的.NET

    在过去数年中,桌面应用开发人员一直处境艰难,因为人们的主要关注点聚焦于Web和移动应用.由于Microsoft未来Windows平台的计划未定,并且UWP应用也没有突破性进展,因此技术落伍的责任也不应 ...

  8. js setInterval()函数 [倒计时用]

    定义和用法:        setInterval() 方法用于在指定的毫秒数后调用函数或计算表达式.它与setTimeout()方法不同的是前者可以无限的循环,不会受调用函数的限制,要想退出此循环可 ...

  9. hbase replication原理分析

    本文只是从总体流程来分析replication过程,很多细节没有提及,下一篇文章准备多分析分析细节.   replicationSource启动过程 org.apache.hadoop.hbase.r ...

  10. Jquery学习笔记(2)--五角星评分

    网购五星评分模拟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...