第一步,导入spring jar包(commons-logging-1.0.4.jar、上传组件commons-fileupload.jar和commons-io.jar)
第二步,项目配置文件web.xml
<?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>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第三步,springMVC配置文件springmvc-servlet.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" > <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.test.controller" /> <!--对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 处理文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="gbk" /> <!-- 默认编码 (ISO-8859-1) --> <property name="maxInMemorySize" value="10240" /> <!-- 最大内存大小 (10240) --> <property name="uploadTempDir" value="/uploadFile/" /> <!-- 上传后的目录名 --> <property name="maxUploadSize" value="-1" /> <!-- 最大文件大小,-1为无限制 --> </bean> </beans>
第四步,控制器FileUploadController.java
package com.test.controller; import java.io.File; import java.util.Date; import javax.servlet.ServletContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.ServletContextAware; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Controller public class FileUploadController implements ServletContextAware { private ServletContext servletContext; public ServletContext getServletContext() { return servletContext; } public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } // 这里@RequestParam("file")必须要有(不清楚为什么),必须是post请求 @RequestMapping(value = "/upload", method = RequestMethod.POST) public String handleUploadData(@RequestParam("file") CommonsMultipartFile file, Model model) { String fileName = ""; String message = ""; if (!file.isEmpty()) { String path = this.servletContext.getRealPath("/temp");// 获取本地存储路径 System.out.println(path); fileName = file.getOriginalFilename(); String[] fileArray = fileName.split("\\."); File file2 = new File(path + "\\" + fileArray[0] + "_" + new Date().getTime() + "." + fileArray[1]); try { file.getFileItem().write(file2); } catch (Exception e) { e.printStackTrace(); message = "上传失败!"; } message = "上传成功!"; } else { message = "没有文件可上传!"; } model.addAttribute("fileName", fileName); model.addAttribute("message", message); return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/uploadFile.jsp"; } }
前台页面/uploadFile.jsp
<%@ page language="java" pageEncoding="gbk"%> <html> <head> <title>测试springmvc中上传的实现</title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传"/> </form> <br> <h1> <% String message = request.getParameter("message"); String fileName = request.getParameter("fileName"); fileName = fileName == null ? "" : fileName; if (!"".equals(fileName)) { fileName = new String(fileName.getBytes("iso8859-1"), "GBK"); } if(message != null){ %> 文件: <% } %> <%=fileName %> <% message = message == null ? "" : message; if (!"".equals(message)) { message = new String(message.getBytes("iso8859-1"), "GBK"); } %> <%=message%> </h1> </body> </html>
在WebContent下面新建temp和uploadFile文件夹,其中,temp是用于存放上传的文件,uploadFile是临时存放上传的文件一部分(相当于缓存)
浏览器输入:
http://127.0.0.1:9900/springmvc_007_upload/uploadFile.jsp
页面显示:
相关推荐
springmvc上传文件所需jar包springmvc上传文件所需jar包 springmvc上传文件所需jar包
springmvc上传文件到ftp demo,将mmall.properties里面的ftp内容修改为自己的启动就行,建议使用idea
个人整理-SpringMVC上传文件的四种方法,汉jar包,含计时
springmvc上传文件controller,vue
springMVC 上传文件方式springMVC 上传文件方式springMVC 上传文件方式
SpringMVC上传图片文件到 阿里云,亲测可用。前端使用Ajax。
springmvc使用CommonsMultipartResolver进行文件上传所需的jar包,包含ssm框架的所需jar包
简化springMvc设置,实现上传,下载功能。
SpringMvc上传本地图片保存数据库并实时显示
文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data。只有在这样的情况下,浏览器才会把用户选择的文件以二进制数据发送给服务器。 一旦设置...
导入eclipse中,运行项目即可,上传文件限制不能超过2G,如果要修改则自己在springmvc.xml文件中修改配置
springMVC上传文件的三种解析方式源码。可以添加到tomcat直接运行,运行访问地址为:http://localhost:8080/SpringMVCUploadFileDemo/upload/toUploadFileView1
springmvc上传下载图片和文件
ajax上传文件,ajax实现进度条,及springMvc上传文件实现进度条
就是简单的一个springmvc上传文件的示例。上传图片后可以显示图片,适合新手学习
springMvc上传下载两个jar包,给需要的人,好东西,物美价廉
由于需要我们需要在项目里自己写文件的上传和下载。这里分别有基于SpringMVC的上传和基于SSH的上传下载,欢迎下载。
在使用springMVC进行系统实现时,springMVC默认的解析器里面是没有加入对文件上传的解析的,这可以方便我们实现自己的文件上传。但如果你想使用springMVC对文件上传的解析器来处理文件上传的时候就需要在spring的...
主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下