【问题标题】:Uploading files in Spring with Tomcat related to the maximum size allowed在 Spring 中使用 Tomcat 上传文件与允许的最大大小有关
【发布时间】:2015-06-23 10:45:32
【问题描述】:

我对 Spring 相当陌生,我想要一个多部分表单并处理 MaxUploadSizeExceededException 异常,以便在 jsp 中显示错误消息。我遇到的主要问题是 MaxUploadSizeLimitExceededExceptionResolver 类中的 ModelAndView 对象,我不知道如何将它用于前面解释的此类目标。

我拥有的文件: 1)模型类UploadItem.java。 2) 查看表单UploadForm.jsp。 3) 控制器上传控制器.java。 4) 类MaxUploadSizeLimitExceededExceptionResolver.java处理异常Uploadcontroller

1) 模型上传项目

public class UploadItem {
    private String name;
    private CommonsMultipartFile fileData;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public CommonsMultipartFile getFileData() {
        return fileData;
    }
    public void setFileData(CommonsMultipartFile fileData) {
        this.fileData = fileData;
    }
}

2) 查看表单 UploadForm.jsp

<html>
    <head>
        <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Upload form</title>
    </head>
    <body>
        <form:form modelAttribute="uploadItem" method="post" enctype="multipart/form-data">
            <fieldset>
                <legend>Upload Fields</legend>
                <p>
                    <form:label for="name" path="name">Name</form:label><br/>
                    <form:input path="name"/>
                </p>
                <p>
                    <form:label for="fileData" path="fileData">File</form:label><br/>
                    <form:input path="fileData" type="file"/>
                </p>
                <p>
                    <input type="submit" />
                </p>
            </fieldset>
        </form:form>
    </body>
</html>

3) 控制器上传控制器

public class Uploadcontroller {
    @RequestMapping(method = RequestMethod.GET)
    public String getUploadForm(Model model) {
        model.addAttribute(new UploadItem());
        return "upload/uploadForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(HttpServletRequest request, UploadItem uploadItem,
            BindingResult result, Exception exception) {
        if (result.hasErrors()) {
            // logger.info("create upload");
            for (ObjectError error : result.getAllErrors()) {
                System.err.println("Error: " + error.getCode() + " - "
                        + error.getDefaultMessage());
            }
            return "upload/uploadForm";
        }

        System.err.println("Test upload: " + uploadItem.getName());
        System.err.println("Test upload: "
                + uploadItem.getFileData().getOriginalFilename());

        // TODO redirect this
        return "/home";
    }
}

4) 处理Uploadcontroller异常的组件

@Component
public class MaxUploadSizeLimitExceededExceptionResolver extends
        SimpleMappingExceptionResolver {
    private static final Logger logger = LoggerFactory
            .getLogger(HomeController.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception exception) {
        Map<String, Object> model = new HashMap<String, Object>();
        logger.info("getUploadForm at the beggining");
        ModelAndView modelView = new ModelAndView();
        if (exception instanceof MaxUploadSizeExceededException)
        {
            model.put("errors", exception.getMessage());
            modelView.addObject("errors", exception.getMessage());
            logger.info("getUploadForm: MaxUploadSizeExceededException" );
        } else
        {
            model.put("errors", "Unexpected error: " + exception.getMessage());
            modelView.addObject("errors", "Unexpected error: " +exception.getMessage());
        }
        logger.info("getUploadForm at the end" );        
        model.put("uploadedFile", new UploadItem());
        modelView.addObject(new UploadItem());//("uploadedFile", new UploadItem());
        modelView.setViewName("/upload/uploadForm");
        return modelView;
    }
}

编辑以添加更多详细信息: 实际上,问题在另一个方向。我的 maxUploadSize 文件的大小设置为 1MB,但我试图对大于 3 MB 的文件进行测试。当我尝试使用一个文件直到最大 2 MB 工作正常时。问题是我得到了一个 ERR_CONNECTION_RESET,它似乎与 Tomcat 有关,maxSwallowSize 的配置 --> stackoverflow.com/questions/29113262/... 我将继续研究并保持更新这个线程。


新信息。 我试过 Tomcat 7.0.61,错误是 ERR_CONNECTION_ABORTED 我用Tomcat 6.0.43试过了,没有错误!

【问题讨论】:

  • 嗨小,谢谢你的回答。不幸的是,问题似乎是另一个问题,请参阅我编辑的条目。因此,您粘贴的那篇文章对这个特定问题没有帮助。问候,内斯特

标签: java spring jsp spring-mvc tomcat


【解决方案1】:

经过调查,问题出在Tomcat上。
In the version 7.0.55 was introduced the property maxSwallowSize默认设置为2MB。这使 Tomcat 中止了上传请求。通过设置这个属性to another value,问题就解决了(我把它改成了-1,请不要在你的PRD环境中这样做,因为Tomcat真的会尝试获取一个X MB的文件来上传)。我通过在我的连接器属性中添加我的 Tomcat 服务器文件 ${tomcat_dir}/conf/server.xml 属性 maxSwallowSize

来做到这一点
<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" 
    maxSwallowSize="-1"/>

你需要重启Tomcat,所以它采用这个配置,如果不起作用,删除服务器并重新添加它。

【讨论】:

  • 我没有看到 7.0.55 的发行说明中提到了 maxUploadSize
  • 另外,你的回答甚至没有显示使用 maxUploadSize。什么是 maxSwallowSize?
  • 谢谢,maxUploadSize写错了,其实是maxSwallowSize
猜你喜欢
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 2014-09-11
  • 2017-10-18
  • 1970-01-01
相关资源
最近更新 更多