Spring +SpringMVC 实现文件上传功能。。。
要实现Spring +SpringMVC 实现文件上传功能。
第一步:下载

第二步:
新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring的jar包这里就不多说)
然后把刚刚下载的两个包放在lib目录下。
第三步:
在web.xml中把springMVC和spring合并。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--配置编码格式-->
<filter>
<filter-name>filterEncoding</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>
</filter>
<filter-mapping>
<filter-name>filterEncoding</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 合并spring mvc的xml配置文件和spring的配置文件 如果没有合并那么每个control 都对应一个xml (servletname-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
第四步:
修改spring配置文件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: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/context
http://www.springframework.org/schema/context/spring-context-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
"> <context:component-scan base-package="com.control"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"/>
</bean>
<!-- 上传文件必须的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean> </beans>
ok!环境就这样搭好了!接下来就真正实现功能了!
首先书写一个上传文件的类
@Controller
@RequestMapping("/file")
public class UploadControl { @RequestMapping("/upload.html")
public String upload(@RequestParam (value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model){ System.out.println("开始");
//文件上传到的位置
String path = request.getSession().getServletContext().getRealPath("upload");
//上传的文件名
String fileName = file.getOriginalFilename();
//处理重复提交相同东西不被覆盖
// String fileName = new Date().getTime()+".jpg";
File targetFile = new File(path, fileName);
//判断文件夹是否存在
if(!targetFile.exists()){
targetFile.mkdirs();
} //保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName); return "success";
}
}
然后是jsp
<body>
<form action="file/upload.html" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form> <br/>
就这样一个上传的功能就实现了!
Spring +SpringMVC 实现文件上传功能。。。的更多相关文章
- SpringMVC实现文件上传功能
文件上传 文件上传要求form表单的请求方式必须为post,并且添加属性enctype="multipart/form-data" SpringMVC中将上传的文件封装到Multi ...
- 基于Spring MVC的文件上传和下载功能的实现
配置文件中配置扫描包,以便创建各个类的bean对象 <context:component-scan base-package="com.neuedu.spring_mvc"& ...
- springmvc中使用文件上传功能
项目代码:https://github.com/PeiranZhang/springmvc-fileupload Servlet3.0之前使用文件上传功能 Servlet3.0之前需要使用common ...
- Spring 文件上传功能
本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...
- spring mvc 3.0 实现文件上传功能
http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...
- Spring MVC实现文件上传
基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...
- Spring MVC的文件上传
1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...
- SpringMVC+ajax文件上传实例教程
原文地址:https://blog.csdn.net/weixin_41092717/article/details/81080152 文件上传文件上传是项目开发中最常见的功能.为了能上传文件,必须将 ...
- Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器
1.文件上传. spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...
随机推荐
- Windows 2003上 SaltStack/Salt 和 psutil 可能存在的问题及解决
今天把salt安装在windows 2003上,发现无法启动,随之而来的是一个有一个的坑,让我们一起逐个排查. 问题一(salt无法启动) salt无法启动,错误结果如图:
- jboss端口说明
http://blog.csdn.net/yangbobo1992/article/details/8876587 jboss端口修改说明 1. jboss 的端口修改位置总结 Jboss通常占用的端 ...
- tony_nginx_02_URL重写
location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的 ...
- go 的 protoc 插件调用逻辑
要让protoc使用插件,需要做下面事情: Place the plugin binary somewhere in the PATH and give it the name "proto ...
- Chap5: question 35 - 37
35. 第一个只出现一次的字符 char firtNotRepeat(char *s) { if(s == NULL) return 0; int i = 0; while(s[i] != '\0') ...
- Android:去掉默认的标题bar
要使用自己定义的bar,只需要在layout文件中添加:<include layout="@layout/actionbar" />;当然你需要新建一个actionba ...
- 关于sublime text2
转自:http://www.qianduan.net/essential-to-sublime-the-text-2-plugins.html Sublime Text 2是一个轻量.简洁.高效.跨平 ...
- Linux学习笔记——使用指定的用户权限执行程序——sudo
sudo可以用来以其他用户身份执行命令,sudo命令可以针对单个命令授予临时权限.sudo仅在需要时授予用户权限,减少了用户因为错误执行命令损坏系统的可能性. 1:sudo的帮助信息如下: ...
- [Android]通过js方法回调部分native报错 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...
- eclipse报错 :java was started but returned exit code=13
一个礼拜没打开自己电脑上的eclipse,一打开居然报错,错误提示如下: cdm运行一下Java -version和javac 运行javac时报错,错误提示如下: 然后发现jdk的bin下没有dt. ...