springmvc文件上传2中方法
基于前面文章的基础上。
一、准备
需要的jar

二、配置
1、 spmvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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"> <!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter-->
<mvc:annotation-driven /> <!-- 自动扫描注解的Controller -->
<context:component-scan base-package="com.wy.controller" /> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 映射处理器 -->
<bean id="simpleUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/fileUploadController.do">fileUploadController</prop>
</props>
</property>
</bean> <!-- ParameterMethodNameResolver 解析请求参数,并将它匹配Controller中的方法名 -->
<bean id="parameterMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action" />
</bean> <bean id="fileUploadController"
class="com.wy.controller.FileUploadController">
<property name="methodNameResolver"
ref="parameterMethodNameResolver">
</property>
</bean> <!-- 文件上传表单的视图解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="204800" />
</bean> </beans>
2、Controller
使用两种方式:
一种是基于注解的,另一种传统的方式HttpServletRequest
使用第二种方式时要注意:操作方法中对应的方法参数前两位必须是request,response对象并且都要加上,否则会出现 No request handling method with name 'insert' in class "ClassName",页面显示为404错误
这个问题出现在使用多操作控制器情况下,相关的操作方法中对应的方法参数前两位必须是request,response对象,必须要有,否则会报如上异常。
package com.wy.controller; import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
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.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; @Controller
@RequestMapping("/fileUploadController")
public class FileUploadController extends MultiActionController { /**
* 1、文件上传
* @param request
* @param response
* @return
*/
public ModelAndView uploadFiles(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
// 转型为MultipartHttpRequest
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得上传的文件(根据前台的name名称得到上传的文件)
MultiValueMap<String, MultipartFile> multiValueMap = multipartRequest.getMultiFileMap();
List<MultipartFile> file = multiValueMap.get("clientFile");
//MultipartFile file = multipartRequest.getFile("clientFile");
if(!file.isEmpty()){
//在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹
System.out.println("================="+file.get(0).getName() + file.get(0).getSize());
} return mav;
} /**
*
* @param name
* @param file
* @param session
* @return
*/
@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
public String uploadFile(@RequestParam("fileName") String fileName,
@RequestParam("clientFile") MultipartFile clientFile, HttpSession session){
if (!clientFile.isEmpty()) {
//在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹
System.out.println("================="+clientFile.getSize());
}
return "";
} }
对文件的具体实现
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; public class AddImage { public String upload2(HttpServletRequest request,HttpServletResponse response, String fileName) throws IllegalStateException, IOException {
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//记录上传过程起始时的时间,用来计算上传时间
int pre = (int) System.currentTimeMillis();
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println(myFileName);
//重命名上传后的文件名
fileName = UUID.randomUUID() +"+"+ file.getOriginalFilename();
//定义上传路径
String path = "F:/workspace/myproject/WebRoot/image/" + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
//记录上传该文件后的时间
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre); }
}
return fileName ;
}
}
3、试图
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>file upload test</title>
</head>
<body> <form method="post" action="<%=path %>/fileUploadController/uploadFile" enctype="multipart/form-data">
文件名: <input type="text" name="fileName" /><br/>
<input type="file" name="clientFile" /><br/>
<input type="submit" value="上传文件 "/>
</form>
</body>
</html>
springmvc文件上传2中方法的更多相关文章
- IIS 7 中设置文件上传大小的方法
在IIS 6.0中设置文件上传大小的方法,就是配置如下节点: <system.web> <httpRuntime maxRequestLength="1918200&quo ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
- springMVC文件上传大小超过限制的问题
[转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...
- 18 SpringMVC 文件上传和异常处理
1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...
- springmvc文件上传AND jwt身份验证
SpringMVC文件上传 思路:1.首先定义页面,定义多功能表单(enctype=“multipart/form-data”)2.在Controller里面定义一个方法,用参数(MultipartF ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...
- 基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式【Options】处理常规上传和FTP文件上传
在基于SqlSugar的开发框架的服务层中处理文件上传的时候,我们一般有两种处理方式,一种是常规的把文件存储在本地文件系统中,一种是通过FTP方式存储到指定的FTP服务器上.这种处理应该由程序进行配置 ...
随机推荐
- jQuery实现用户注册的表单验证
用户注册的表单往往是需要进行验证的,否则会有一些不否合规则的数据入库,后果会不堪设想,本文通过jquery来实现. <html> <head> <meta chars ...
- CSS content内容生成技术以及应用
content属性早在CSS2.1的时候就被引入了,可以使用:before以及:after伪元素生成内容.此特性目前已被大部分的浏览器支持:(Firefox 1.5+, Safari 3.5+, IE ...
- php win主机下实现ISAPI_Rewrite伪静态
有的win主机iss不支持 .htaccess 文件, 我在这里指的不是本地 在本地的话用apmserv服务器可以用.htaccess 文件,用apmserv服务器环境配置伪静态可以看 php 伪静态 ...
- TextView中gravity属性值测定
Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...
- How to begin Python learning?
如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...
- Java从入门到精通——基础篇之Servlet与JSP的区别
一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...
- 【ajax跨域】原因原理解决
1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...
- iOS开发应用学习笔记
一.iOS应用设计 1. 参考资料: 解读iPhone平台的一些优秀设计思路 iPhone App的特点及基本设计方法 Mobile UI design and Developer 2. 用户对iPh ...
- 人工智能起步-反向回馈神经网路算法(BP算法)
人工智能分为强人工,弱人工. 弱人工智能就包括我们常用的语音识别,图像识别等,或者为了某一个固定目标实现的人工算法,如:下围棋,游戏的AI,聊天机器人,阿尔法狗等. 强人工智能目前只是一个幻想,就是自 ...
- Java中List和ArrayList的区别
List:是一个有序的集合,可以包含重复的元素.提供了按索引访问的方式.它继承 Collection.List有两个重要的实现类:ArrayList 和 LinkedListArrayList:我们可 ...