springMVC處理multipart/form-data類型的表單數據

一、前端作好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>

 

二、在applicationContext.xml中進行配置web

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

三、controller中直接獲取spring

/**
     * 測試是否能上傳文件
     * 
     * @return
     */
    @RequestMapping(value = "/fileUploadTest")
    @ResponseBody
    public String fileUploadTest(String device, String localDate, MultipartFile file1) {
        System.out.println(device + "," + localDate + "," + file1);return "yes";
    }
相關文章
相關標籤/搜索