springmvc如何拦截上传文件最大限制异常

springmvc如何拦截上传文件最大限制异常,第1张

在applicationContext.xml中添加: <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --><property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 --><property name="defaultEncoding" value="utf-8"></property><property name="resolveLazily" value="true"></property></bean><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --><property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 --><property name="defaultEncoding" value="utf-8"></property><property name="resolveLazily" value="true"></property></bean>只需在控制层 @Excep... 在applicationContext.xml中添加:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->

<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->

<property name="defaultEncoding" value="utf-8"></property>

<property name="resolveLazily" value="true"></property>

</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->

<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->

<property name="defaultEncoding" value="utf-8"></property>

<property name="resolveLazily" value="true"></property>

</bean>

只需在控制层

@ExceptionHandler

public ModelAndView doException(Exception e, HttpServletRequest request) throws Exception {

Map<String, Object>map = new HashMap<String, Object>()

if (e instanceof MaxUploadSizeExceededException) {

long maxSize = ((MaxUploadSizeExceededException) e).getMaxUploadSize()

map.put("error", "上传文件太大,不能超过" + maxSize / 1024 + "k")

// response.setHeader("Content-type", "text/htmlcharset=UTF-8")

// // 设置默认编码

// response.setCharacterEncoding("UTF-8")

// response.getWriter().write("上传文件太大,不能超过" + maxSize / 1024 + "k")

} else if (e instanceof RuntimeException) {

map.put("error", "未选中文件")

} else {

map.put("error", "上传失败")

}

return new ModelAndView("upload", map)

}

@ExceptionHandler

public ModelAndView doException(Exception e, HttpServletRequest request) throws Exception {

Map<String, Object>map = new HashMap<String, Object>()

if (e instanceof MaxUploadSizeExceededException) {

long maxSize = ((MaxUploadSizeExceededException) e).getMaxUploadSize()

map.put("error", "上传文件太大,不能超过" + maxSize / 1024 + "k")

// response.setHeader("Content-type", "text/htmlcharset=UTF-8")

// // 设置默认编码

// response.setCharacterEncoding("UTF-8")

// response.getWriter().write("上传文件太大,不能超过" + maxSize / 1024 + "k")

} else if (e instanceof RuntimeException) {

map.put("error", "未选中文件")

} else {

map.put("error", "上传失败")

}

return new ModelAndView("upload", map)

}

即可拦截到上传文件大小

springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便。

我们通过下面的配置来使用springMVC文件上传功能。

<bean id="multipartResolver" class="com.youth.controller.fileupload.MultipartListenerResolver">

<!-- 设置上传文件的最大尺寸为10M -->

<property name="maxUploadSize" value="10240"/>

<property name="maxInMemorySize" value="4096"/>

<property name="defaultEncoding" value="UTF-8"/>

<property name="resolveLazily" value="true"/> </bean>

Controller层我们这样接收文件

@RequestMapping("fileUpload")public void fileUpload(@RequestParam("myFile") MultipartFile multipartFile) throws Exception{

String fileName = multipartFile.getOriginalFilename()

File f = createFile(fileName)

multipartFile.transferTo(f)

}

页面上记得指定enctype属性哦

<form method="post" enctype="multipart/form-data"></form>

如果你的springMVC配置是正确的,那么到此springMVC的文件上传功能已经可以用了。一切都很完美。

由于我们配置了maxUploadSize属性,那么如果我们的文件超过了10M会出现什么情况呢?

理论上系统会抛出MaxUploadSizeExceededException异常,那么如何处理呢?

springMVC异常处理的配置方式如下:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="exceptionMappings">

<props>

<prop key="java.lang.Exception">redirect:/error.jsp</prop>

<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">redirect:/MaxUploadSizeError.jsp</prop>

</props> </property><property name="defaultErrorView" value="redirect:/error.jsp"></property><property name="defaultStatusCode" value="500"></property></bean>

上面的配置作用是如果系统抛出MaxUploadSizeExceededException异常,系统跳转到MaxUploadSizeError.jsp页面给用户以提示。

如果是其他Exception异常,则跳转到error.jsp页面。

接下来我们测试一下上面的异常处理是否生效了。

在你的代码任意地方试着抛出NullPointException异常,发现页面的确能跳转到error.jsp,证明我们的异常处理是生效的。

然后我们上传一个10M以上的文件,我们发现后台控制台抛出了MaxUploadSizeExceededException异常,我们期待着页面转向到MaxUploadSizeError.jsp。

SpringMVC的文件上传非常简便,首先导入文件上传依赖的jar:

<!-- 文件上传所依赖的jar包 -->

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3.1</version>

</dependency>

在springMVC-servlet.xml配置文件中配置文件解析器:

<!--1*1024*1024即1M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 -->

<!--文件上传解析器-->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="1048576"/>

<property name="defaultEncoding" value="UTF-8"/>

<property name="resolveLazily" value="true"/>

</bean>

注意解析器的id必须等于multipartResolver,否则上传会出现异常

import org.apache.commons.io.FileUtils

import org.springframework.stereotype.Controller

import org.springframework.web.bind.annotation.RequestMapping

import org.springframework.web.bind.annotation.RequestParam

import org.springframework.web.multipart.MultipartFile

import org.springframework.web.multipart.commons.CommonsMultipartFile

import java.io.File

@Controller

public class FileController {

/**

* 上传单个文件 *** 作

* MultipartFile file就是上传的文件

* @return

*/

@RequestMapping(value = "/upload1.html")

public String fileUpload1(@RequestParam("file") MultipartFile file) {

try {

//将上传的文件存在E:/upload/下

FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/upload/",

file.getOriginalFilename()))

} catch (Exception e) {

e.printStackTrace()

}

//上传成功返回原来页面

return "/file.jsp"

}}

上传文件时,Controller的方法中参数类型是MultipartFile即可将文件映射到参数上。

页面:

file.jsp:

<form method="post" action="/upload1.html" enctype="multipart/form-data">

<input type="file" name="file"/>

<button type="submit" >提交</button>

</form>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11589747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存