我想上傳一個使用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">
我挖的所有網絡,但無法解決此問題。我的錯是什麼?
什麼是文件BrandController線75內容的.java? – 2015-02-24 21:30:20
行是if(!brandDTO.getFile()。isEmpty()){//空指針異常 – katsu 2015-02-24 21:41:41