2014-07-19 36 views
1

我在使用ExtJS和Spring 4.0.5以及RepositoryRestMVC功能上傳文件時出現問題。Spring + ExtJS文件上傳必需MultipartFile參數「文件」不存在

簡而言之,我有一個由ExtJS創建的表單,並將POST請求提交給Spring MVC後端。

我已經安裝Spring使用java配置,沒有xml文件。

這裏是我的ApplicationInitializer.class春:

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); 
} 

}

這裏是我的WebMvcConfig.class

@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; 
    } 

} 

這裏是FileUploadController.class

@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; 
    } 
} 

UploadedFile.class是這樣的:

public class UploadedFile implements Serializable { 

    private static final long serialVersionUID = -38331060124340967L; 
    private String name; 
    private Integer size; 
    private String url; 
    private String thumbnail_url; 
    private String delete_url; 
    private String delete_type; 

    public UploadedFile() { 
     super(); 
    } 

    public UploadedFile(String name, Integer size, String url) { 
     super(); 
     this.name = name; 
     this.size = size; 
     this.url = url; 
    } 

    public UploadedFile(String name, Integer size, String url, 
     String thumbnail_url, String delete_url, String delete_type) { 
     super(); 
     this.name = name; 
     this.size = size; 
     this.url = url; 
     this.thumbnail_url = thumbnail_url; 
     this.delete_url = delete_url; 
     this.delete_type = delete_type; 
    } 

    //getter and setters 

    @Override 
    public String toString() { 
     return "UploadedFile [name=" + name + ", size=" + size + ", url=" + url 
      + ", thumbnail_url=" + thumbnail_url + ", delete_url=" 
      + delete_url + ", delete_type=" + delete_type + "]"; 
    } 

} 

在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錯誤的請求的響應

必需MultipartFile參數「文件」不存在

的援助任何形式將不勝感激。

願原力與你同在

+0

解決您的問題,您可以嘗試此解決方案:http://stackoverflow.com/a/25834298/4017037 – stacky

+0

你是怎麼最終解決問題了嗎? – sina

回答

0

我得到了在ApplicationInitializer類擺脫上傳的configs的,而是創造了一個上傳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> 

相關問題