Spring MVC-分段上传的图片返回null

问题描述 投票:0回答:1

我正在使用Spring MVC和Multipart上载图像。但是它总是返回null。我已经设置了所有配置,直到现在我仍无法弄清原因。有这种经验的人请帮助我。非常感谢。

Pom.xml

<dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>    
</dependency>   
<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>  
</dependency>

bean xml

<beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       <beans:property name="maxUploadSize" value="167772160" />
    </beans:bean>

@ Controller

@RequestMapping(value = "/data/edit/request", method = RequestMethod.POST, produces = Constant.MULTIDATA_CHARSET_UTF_8)
    public String updateData(Model model, HttpSession session,HttpServletRequest request, RedirectAttributes redirectAttrs,
            @ModelAttribute RawUserDto rawUserDto) {    
        try {
            int rawUserSn = userService.insertRawUser(rawUserDto.toRawUser());
            if(rawUserSn > 0){//insert new raw users
                //to-do
            }
        } catch (Exception e) {
            //Write log and redirect to error page
            LOG.error(e);
        }
        return "inputData";
    }

RawUserDto

public class RawUserDto implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    private String email;

    private String birthday;

    private int gender;

    @JsonIgnore
    private MultipartFile avatar;
}

JSP文件

<form:form  class="form-horizontal" action="${pageContext.request.contextPath}/data/edit/request" method="POST" role="form" modelAttribute="rawUserDto" enctype="multipart/form-data">


    <div class="form-group">
        <label class="control-label col-lg-2">Avatar <span class="text-danger">*</span></label>
        <div class="col-lg-10">
            <form:input type="file" path="avatar" name="avatar" class="file-styled" required="required" accept="jpg;gif;png;bmp;jpeg"/> 
        </div>
    </div>
</form:form>

有关详细信息,您可以看到我从屏幕上捕获的图像。如您所见,我可以得到的其他信息只有多部分数据为NULL。

Debug mode by screen captured

spring-mvc file-upload multipart
1个回答
0
投票

保存图像

public void createProduct(User user, MultipartFile[] files) throws IOException {
String rootDir = System.getProperty("user.dir")+"/src/main/webapp/static/images/items";
    for (MultipartFile file : files){

        Path path = Paths.get(rootDir, file.getOriginalFilename());
        String filename = file.getOriginalFilename();

        try{

            Files.write(path, file.getBytes());

        }catch (IOException e) {

            e.printStackTrace();

            throw new MultipartException("You got an Error men");

        }

        user.setAvatar(filename);

    }
    rawUserDtoRepository.save(user);

}

公共类RawUserDto实现java.io.Serializable {

private static final long serialVersionUID = 1L;

private String name;

private String email;

private String birthday;

private int gender;

@JsonIgnore
private String avatar;

}

将图像文件保存在静态文件夹中,就像js CSS一样

© www.soinside.com 2019 - 2024. All rights reserved.