2015-02-24 190 views
1

我想上传一个使用Spring mvc,tomcat,Tyhmleaf的多部分文件,但不能得到它的工作。春季多部分文件上传

java.lang.NullPointerException 
com.cars.actions.controller.brand.BrandController.persist2(BrandController.java:75) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:483) 

我的控制器:

@RequestMapping(value = "/addimage", method = RequestMethod.POST) 
public String persist2( @Valid @ModelAttribute("brand") BrandDTO brandDTO, BindingResult result, RedirectAttributes redirectAttrs, Model model) { 

如果(result.hasErrors()){

 if (!brandDTO.getFile().isEmpty()) { // null pointer exception 

     errorNotValid(model, ""); 
     return ADD_PAGE; 
    } else { 

尝试{ BufferedImage的SRC = ImageIO.read(新ByteArrayInputStream的(brandDTO.getFile( ).getBytes())); File destination = new File(“/ Users/katsu/Desktop/file /”); ImageIO.write(src,“png”,destination); } catch(IOException e){TODO自动生成的catch块 e.printStackTrace(); } } try { Brand brand = getEntity(brandDTO); service.save(品牌); successAddMesage(model,“succes”); model.addAttribute(businessObject,new Brand()); return ADD_PAGE; (DuplicateItemFoundException e){ errorDuplicateMessage(redirectAttrs,“dublucate”); return REDIRECT_LIST_PAGE; }

} 
} 

我的DTO:

public class BrandDTO { 

private Integer id; 
private String name; 
private boolean enabled; 
private CommonsMultipartFile file; 

Apliaiton iniliaze:

public class ApplicationInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 

    // Register the Root application context 
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
    rootContext.register(RootConfig.class); 

    // Register the Web application context 
    AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext(); 
    mvcContext.register(WebAppConfig.class); 

    // Context loader listener 
    //servletContext.addListener(new ContextLoaderListener(rootContext)); 

    // Register the Dandelion filter 
    FilterRegistration.Dynamic dandelionFilter = servletContext.addFilter("dandelionFilter", new DandelionFilter());  
    dandelionFilter.addMappingForUrlPatterns(null, false, "/*"); 

    // Register the Spring dispatcher servlet 
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springServlet", new DispatcherServlet(mvcContext)); 
    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("/"); 
    //dispatcher.setMultipartConfig(rootContext.getBean(Multi)); 

    // Register the Dandelion servlet 
    ServletRegistration.Dynamic dandelionServlet = servletContext.addServlet("dandelionServlet",new DandelionServlet()); 
    dandelionServlet.setLoadOnStartup(2); 
    dandelionServlet.addMapping("/dandelion-assets/*"); 

} 

我的网络初始化:

@Bean 
public CommonsMultipartResolver multipartResolver() { 
    CommonsMultipartResolver resolver=new CommonsMultipartResolver(); 
    resolver.setDefaultEncoding("utf-8"); 
    resolver.setMaxUploadSize(40000000); 
    return resolver; 
} 

我的表格:

<form class="mainForm" action="#" data-th-action="@{/admin/brand/addimage}" data-th-object="${brand}" method="post" accept-charset="utf-8" enctype="multipart/form-data"> 
      <!-- Input text fields --> 
      <fieldset> 
       <div class="widget first"> 
        <div class="head"> 
         <h5 class="iList">Marka Ekleme</h5> 
        </div> 
        <div class="rowElem"> 
         <label>Marka Adı:</label> 

         <div class="formRight"> 
          <input type="text" placeholder="adını giriniz" data-th-value="*{name}" data-th-field="*{name}" /> 
          <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</span> 
         </div> 
         <div class="fix"></div> 
        </div> 
        <div class="rowElem"> 
         <label>Marka Logo:</label> 

         <div class="formRight"> 
          <input type="file" data-th-field="*{file}"/> 
         </div> 
         <div class="fix"></div> 
        </div> 

        <div class="rowElem"> 
         <label>Aktif Mi:</label> 

         <div class="formRight"> 
          <select class="select2" title="Click to Select a City" th:field="*{enabled}"> 
           <option th:each="type : ${enabledOptions}" th:value="${type}" th:text="${type}">Dropdown</option> 
          </select> 
         </div> 
         <div class="fix"></div> 
        </div> 

        <input type="submit" value="Submit form" class="greyishBtn submitForm" /> 
        <div class="fix"></div> 
       </div> 
      </fieldset> 
     </form> 

和最后tomcat的权限:

<Context allowCasualMultipartParsing="true"> 

我挖的所有网络,但无法解决此问题。我的错是什么?

+0

什么是文件BrandController线75内容的.java? – 2015-02-24 21:30:20

+0

行是if(!brandDTO.getFile()。isEmpty()){//空指针异常 – katsu 2015-02-24 21:41:41

回答

0

尝试,因为没有明确的命名它从参数类型派生与

@ModelAttribute("brand") BrandDTO brandDTO 

目前你的控制器需要名为“brandDTO”模型属性,是空取代

@ModelAttribute BrandDTO brandDTO 

。但在你的表单中,你设置了一个data-th-object = $ {brand}。

相反,如果你保持brandDTO名称属性的,你应该改变重命名你个对象在页面:

data-th-field="${brand.file}" 

到:

data-th-field="*{file}" 
+0

它没有工作。其他值不为null。只有多部分文件为空。 – katsu 2015-02-25 14:42:52

+0

将有问题的if语句和所有封闭代码移入result.hasErrors()条件的其他内部。然后,您可以在评估之前检查是否有任何错误 – grid 2015-02-25 15:03:13

+0

我更新了我的答案。如果您在页面中重命名而不是控制器 – grid 2015-02-25 15:52:43