SpringMVC文件上传
1、pom.xml追加
commons-io
commons-fileupload
2、dispatcherservlet-servlet.xml追加
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2000000" /> <property name="defaultEncoding" value="utf-8" /> </bean>
3、html引入Jquery Form插件
<script type="text/javascript" src="/lib/jquery.form.min.js"></script>
4、示例
HTML
<form id="file-upload-form" class="hide" method="post" enctype="multipart/form-data"> <input name="file" type="file" /> <input name="id" type="hidden"value="1" /> <button onclick="uploadFile()">Upload File</button> </form>
JS
function uploadFile() { $('#file-upload-form').ajaxSubmit({ type: "post", url: '/file_upload', dataType: "json", success: function(resp) { alert("a"); } }); }
Java
/** * 测试<br> * 异步上传文件 * * @author Deolin */ @RequestMapping(value = "file_upload", method = RequestMethod.POST) @ResponseBody public void file_upload(@RequestParam MultipartFile file) { LOG.info("上传"); }