栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何处理MaxUploadSizeExceededException

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何处理MaxUploadSizeExceededException

我终于找到了一个可以使用HandlerExceptionResolver的解决方案。

将多部分解析器添加到你的Spring配置中:

<bean id="multipartResolver" >       <!--  the maximum size of an uploaded file in bytes -->   <!-- <property name="maxUploadSize" value="10000000"/> -->   <property name="maxUploadSize" value="1000"/></bean>   

Mode-UploadedFile.java:

package com.mypkg.models;import org.springframework.web.multipart.commons.CommonsMultipartFile;public class UploadedFile{    private String title;    private CommonsMultipartFile fileData;    public String getTitle()    {        return title;    }    public void setTitle(String title)    {        this.title = title;    }    public CommonsMultipartFile getFileData()    {        return fileData;    }    public void setFileData(CommonsMultipartFile fileData)    {        this.fileData = fileData;    }}

View -/upload.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>    <head>        <title>Test File Upload</title>    </head>    <body>        <h1>Select a file to upload</h1>        <c:if test="${not empty errors}"> <h2 >${errors}.</h2>        </c:if>        <form:form modelAttribute="uploadedFile" method="post" enctype="multipart/form-data" name="uploadedFileform" id="uploadedFileform"> <table width="600" border="0" align="left" cellpadding="0" cellspacing="0" id="pdf_upload_form">     <tr>         <td width="180"><label >Title:</label></td>         <td width="420"><form:input id="title" path="title" cssClass="areaInput" size="30" maxlength="128"/></td>     </tr>     <tr>         <td width="180"><label >File:</label></td>         <td width="420"><form:input id="fileData" path="fileData" type="file" /></td>      </tr>      <tr>         <td width="180"></td>         <td width="420"><input type="submit" value="Upload File" /></td>      </tr> </table>        </form:form>    </body></html>

Controller -FileUploadController.java:包com.mypkg.controllers;

import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.multipart.MaxUploadSizeExceededException;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import com.mypkg.models.UploadedFile;@Controllerpublic class FileUploadController  implements HandlerExceptionResolver{    @RequestMapping(value = "/upload", method = RequestMethod.GET)    public String getUploadForm(Model model)    {        model.addAttribute("uploadedFile", new UploadedFile());        return "/upload";    }    @RequestMapping(value = "/upload", method = RequestMethod.POST)    public String create(UploadedFile uploadedFile, BindingResult result)    {        // Do something with the file        System.out.println("#########  File Uploaded with Title: " + uploadedFile.getTitle());        System.out.println("#########  Creating local file: /var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename());        try        { InputStream in = uploadedFile.getFileData().getInputStream(); FileOutputStream f = new FileOutputStream(         "/var/test-file-upload/" + uploadedFile.getFileData().getOriginalFilename()); int ch = 0; while ((ch = in.read()) != -1) {     f.write(ch); } f.flush(); f.close();        }        catch (IOException e)        { e.printStackTrace();        }        return "redirect:/";    }        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)    {     Map<String, Object> model = new HashMap<String, Object>();        if (exception instanceof MaxUploadSizeExceededException)        { model.put("errors", exception.getMessage());        } else        { model.put("errors", "Unexpected error: " + exception.getMessage());        }        model.put("uploadedFile", new UploadedFile());        return new ModelAndView("/upload", model);    }}========================================================================


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号