【问题标题】:Spring MVC - Multipart Upload Image return nullSpring MVC - 分段上传图片返回 null
【发布时间】:2020-09-26 05:32:38
【问题描述】:

我正在使用 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>

@控制器

@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】:

    保存图片

    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

    【讨论】:

    • 嗨@viray,你把这个函数(createProduct)放在源代码中的什么地方,你是否使用ModelAttribute来获取多部分文件?
    猜你喜欢
    • 2019-09-21
    • 2016-05-11
    • 1970-01-01
    • 2017-05-05
    • 2021-12-31
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多