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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM