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>
Spring Mvc 上传文件Demo 实例的更多相关文章
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- Spring MVC上传文件原理和resolveLazily说明
问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...
- 解析Spring MVC上传文件
新建一个普通的maven工程 在pom.xml文件中引入相应的坐标 <?xml version="1.0" encoding="UTF-8"?> & ...
- spring MVC上传文件演示
//相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...
- MVC:上传文件
今天写了一个使用MVC上传的DEMO,很简单不超过10行代码.代码如下(关注重点,所以尽量精简掉其他代码): 项目结构
- springboot(十七):使用Spring Boot上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...
- (转)Spring Boot(十七):使用 Spring Boot 上传文件
http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
随机推荐
- ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)
在用apt-get安装软件时出现了类似于 install-info: No dir file specified; try –help for more information.dpkg:处理 get ...
- 【微信小程序】退款功能教程(含申请退款和退款回调)
1.一定要区分小程序和公众号的退款,唯一的区别就是 appid不一样,其他的都是一样的. 不废话,直接写代码了啊. 放大招!!! 然后,需要注意的:最好是把证书放在下面的php的同级或者下级. 证书的 ...
- linux 下vi /vim 中文汉字乱码解决
http://my.oschina.net/laserdance/blog/53474很多win下编译的配置文件(译码格式有utf8/gbk)上传到linux服务器上时打开汉字乱码 解决方法如下: 修 ...
- 安装Windows Service总是发生异常!
打开VS2010 创建个windows服务应用程序!没有添加删除任何一行代码!然后按照下面的步骤 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的Proj ...
- SQL server账号无法登陆
- 【JAVA设计模式】外观模式(Facade Pattern)
一 定义 为子系统中的一组接口提供一个一致的界面.Facade模式定义了一个高层的接口,这个接口使得这一子系统更加easy使用. 二 案例 一个子系统中拥有3个模块.每一个模块中都有3个方法.当中 ...
- C#通过SFTP协议操作文件
本文主要是C#调用SSH实现文件上传下载功能,主要是要引用第三方类库Tamir.SharpSSH.dll. 以下是SFTPHelper类,实现了对文件的操作,可供参考. public class SF ...
- c# 多线程里面创建byte数组发生内存溢出异常求解
在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...
- glibc中malloc的详细解释_转
glibc中的malloc实现: The main properties of the algorithms are:* For large (>= 512 bytes) requests, i ...
- hdu6055 Regular polygon 脑洞几何 给定n个坐标(x,y)。x,y都是整数,求有多少个正多边形。因为点都是整数点,所以只可能是正四边形。
/** 题目:hdu6055 Regular polygon 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意:给定n个坐标(x,y).x,y都 ...