如何使用Spring MVC添加多部分配置 [英] How to add multi part config using Spring MVC

查看:115
本文介绍了如何使用Spring MVC添加多部分配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring 将多部分文件发送到控制器,当我将数据发布到URL时,出现此错误:

I'm trying to send multi-part file to a controller using Spring and when I post my data to the URL I get this error :

由于未提供多零件配置,因此无法处理零件.

Unable to process parts as no multi-part configuration has been provided.

我已经在所有地方进行了绝对搜索,但问题仍然悬而未决...我不知道自己在做什么错.

I have searched absolutely everywhere and my problem is still pending... I don't know what I'm doing wrong.

这是我的一些代码:

上传控制器

@Controller
public class UploadController {

        @RequestMapping(value="/upload", method= RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(MultipartHttpServletRequest request, Principal principal){


        User activeUser = principal == null ? null
                : (User) ((Authentication) principal).getPrincipal();


       return "whatever";
    }
}

Application-servlet.xml

<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="100000"/>
</bean>

似乎我已经放好了所有物品.我正在Tomcat 8服务器上运行此应用程序.

It seems that I have the whole inclusions I needed to put. I'm running this app on a Tomcat 8 server.

编辑

按照Sotirios Delimanolis和zeroflagL给出的提示,我的代码已更新.

Following the tips given by Sotirios Delimanolis and zeroflagL, my code was updated.

我进行了几次测试,当我尝试上传超出限制的文件时,我得到了:

I've made several tests and when I'm trying to upload a bigger file than the limit allows, I get this :

最大上传大小超过100000字节;嵌套的异常是org.apache.commons.fileupload.FileUploadBase $ SizeLimitExceededException:请求被拒绝,因为其大小(229305)超出了配置的最大值(100000)

Maximum upload size of 100000 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (229305) exceeds the configured maximum (100000)

这意味着已成功到达multipartResolver,不是吗?

That means the multipartResolver was successfully reached, isn't it ?

但是,我仍然收到 未提供多部分配置文件 的错误.那怎么可能呢?

However, I'm still getting the no multi-part config file provided error. How could that be possible ?

提前谢谢!

推荐答案

我的问题通过以下步骤解决了:

My problem solved with the below steps :

第一步:

要使用 CommonsMultipartResolver 处理文件上传,我们需要添加以下依赖项:

To use CommonsMultipartResolver to handle the file upload, we need to add the following dependency:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
     <version>1.3.1</version>
</dependency>

第二步:

现在,我们可以在我们的Spring配置中定义CommonsMultipartResolver bean.

Now we can define the CommonsMultipartResolver bean into our Spring configuration.

此MultipartResolver带有一系列set方法,用于定义属性(例如,上传的最大大小):

This MultipartResolver comes with a series of set method to define properties such as the maximum size for uploads:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(100000);
    return multipartResolver;
}

第3步:

<form:form method="POST" action="/spring-mvc-xml/uploadFile" enctype="multipart/form-data">
        <table>
            <tr>
                <td><form:label path="file">Select a file to upload</form:label></td>
                <td><input type="file" name="file" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>

第四步:

我们可以从控制器方法内的request参数中检索此变量:

We can retrieve this variable from the request parameter inside our controller's method:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public String submit(@RequestParam("file") MultipartFile file, ModelMap modelMap) {
        modelMap.addAttribute("file", file);
        return "fileUploadView";
    }

这篇关于如何使用Spring MVC添加多部分配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆