springMVC处理multipart/form-data类型的表单数据

1、前端做好form表单

<form action="${pageContext.request.contextPath}/fileUploadTest"
        method="post" enctype="multipart/form-data">
        <label>编号:</label> <input type="text" name="device" id="device">
        <br> <label>日期:</label> <input type="datetime-local"
            name="localDate" id="localDate"> <br> <label>选择文件:</label>
        <input type="file" name="file1" id="file1"> <br> <input
            type="submit" id="fileUpdate" value="点击上传文件"> <br> <input
            type="hidden" id="ssFile" name="ssFile">
    </form>

2、在applicationContext.xml中进行配置

    <!-- 用于文件上传、下载的配置 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="2097152"></property>
    </bean>

3、controller中直接获取

/**
     * 测试是否能上传文件
     * 
     * @return
     */
    @RequestMapping(value = "/fileUploadTest")
    @ResponseBody
    public String fileUploadTest(String device, String localDate, MultipartFile file1) {
        System.out.println(device + "," + localDate + "," + file1);return "yes";
    }

猜你喜欢

转载自www.cnblogs.com/jndx-ShawnXie/p/11747393.html