2015-02-24 179 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