Java 弹簧&x2B;ExtJS文件上传所需的MultipartFile参数';文件';不在场

Java 弹簧&x2B;ExtJS文件上传所需的MultipartFile参数';文件';不在场,java,spring,extjs,spring-data-rest,Java,Spring,Extjs,Spring Data Rest,我在使用带有RepositoryRestMVC功能的Spring4.0.5的ExtJS上传文件时遇到问题 简单地说,我有一个由ExtJS创建的表单,并将POST请求提交给Spring MVC后端 我使用java配置安装了Spring,没有xml文件 这是我的应用程序初始化器。春天的课程: public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { priva

我在使用带有RepositoryRestMVC功能的Spring4.0.5的ExtJS上传文件时遇到问题

简单地说,我有一个由ExtJS创建的表单,并将POST请求提交给Spring MVC后端

我使用java配置安装了Spring,没有xml文件

这是我的应用程序初始化器。春天的课程

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[]{RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[]{};
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}

@Override
protected Filter[] getServletFilters() {
    return new Filter[]{new HiddenHttpMethodFilter(), new MultipartFilter()};
}

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {        
    File uploadDirectory = RootConfig.APP_STORAGE_UPLOADS_DIRECTORY;

    MultipartConfigElement multipartConfigElement = new MultipartConfigElement(
            uploadDirectory.getAbsolutePath(), maxUploadSizeInMb,
            maxUploadSizeInMb * 2, maxUploadSizeInMb / 2);
    registration.setMultipartConfig(multipartConfigElement);
}
这是文件上载控制器。类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.app.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
     }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {       
        System.out.println("===========>>> DispatcherCOntext multipartResolver Called:");
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(7000000);
        return multipartResolver;
    }

}
@Controller
public class FileUploadController {

    private final Logger LOG = LoggerFactory.getLogger(getClass());

    @RequestMapping(value="/user/{id}/photo", method=RequestMethod.POST)
    public @ResponseBody List<UploadedFile> upload(
        @RequestParam("file") MultipartFile file) {
        // Do custom steps here
       // i.e. Save the file to a temporary location or database
        LOG.debug("Writing file to disk...done");

        List<UploadedFile> uploadedFiles = new ArrayList<UploadedFile>();
        UploadedFile u = new UploadedFile(file.getOriginalFilename(),
            Long.valueOf(file.getSize()).intValue(),
            "http://localhost:8080/AppPhoto/resources/"+file.getOriginalFilename());

        uploadedFiles.add(u);
        return uploadedFiles;
    }
}
让我们先看看ExtJS上传表单如下:

Ext.define('FHR.view.MyForm11', {
extend: 'Ext.form.Panel',
requires: [
    'Ext.form.field.File',
    'Ext.toolbar.Toolbar',
    'Ext.button.Button'
],
title: 'My Form',
url:'user/1/photo',
initComponent: function() {
    var me = this;
    Ext.applyIf(me, {
        items: [
            {
                xtype: 'filefield',
                anchor: '100%',
                fieldLabel: 'Profile Photo',
                name: 'file',
                allowBlank: false
            }
        ],
        dockedItems: [
            {
                xtype: 'toolbar',
                dock: 'bottom',
                items: [
                    {
                        xtype: 'button',
                        handler: function(button, e) {   
                            var form = button.up('form').getForm();
                            if (form.isValid()) {
                                form.submit({
                                    success: function(form, action) {
                                        // show message alert box
                                    },
                                    failure: function(form, action) {
                                        // show message alert box
                                    }
                                });
                            } else { // display error alert if the data is invalid
                               // show message alert box
                            }
                        },
                        text: 'Upload'
                    }
                ]
            }
        ]
    });
问题出在这里

在提交POST请求时,我得到了400个错误请求响应

所需的多部分文件参数“file”不存在

任何形式的协助都将不胜感激


愿原力与你同在

我去掉了ApplicationInitializer类中的Upload配置,而是创建了一个Upload xml配置……尽管只想使用Java配置,但它显然是有效的。在下面找到上传配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    http://www.springframework.org/schema/context" http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="7000000" />
</bean>   


要解决您的问题,您可以尝试以下解决方案:您最终是如何解决问题的?
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    http://www.springframework.org/schema/context" http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="7000000" />
</bean>