Spring 分段文件上传

Spring Multipart File Upload

我正在尝试使用 Spring MVC、tomcat、Tyhmleaf 上传 MultipartFile,但无法正常工作。

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) {

if (result.hasErrors()) {

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

        errorNotValid(model, "");
        return ADD_PAGE;
} else {
try {
            BufferedImage src = ImageIO.read(new ByteArrayInputStream(brandDTO.getFile().getBytes()));
            File destination = new File("/Users/katsu/Desktop/file/");
         ImageIO.write(src, "png", destination);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     } 
        try {
            Brand brand = getEntity(brandDTO);
            service.save(brand);
            successAddMesage(model, "succes");
            model.addAttribute(businessObject, new Brand());
            return ADD_PAGE;
        } catch (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;

应用初始化:

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"> 

我搜索了所有互联网但无法解决此问题。我怎么了?

尝试替换

@ModelAttribute BrandDTO brandDTO

@ModelAttribute("brand") BrandDTO brandDTO

目前您的控制器需要一个名为 "brandDTO" 的模型属性,该属性为空,因为没有显式命名它是从参数类型派生的。但是在你的表单中你设置了一个data-th-object=${brand}。

如果您保留属性的 brandDTO 名称并重命名页面中的第 th 个对象,您最好更改:

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

至:

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