http://blog.csdn.net/a1314517love/article/details/24183273
- Step 1: Configure spring
<!-- Upload the multipart file configuration used --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <!--1024*1024*10000 is 1000M--> <property name="maxUploadSize" value="200000" /> <property name="maxInMemorySize" value="40960" /> <!--resolveLazily property is enabled to defer file resolution in order to catch file size exceptions in UploadAction --> <property name="resolveLazily" value="true"/> </bean>
- Step 2: Catch the exception yourself in the upload action
@RequestMapping public void execute( @RequestParam(required = false) MultipartFile file, @RequestParam(value = "file_info_id", required = false) Integer fileInfoId, ModelMap model, HttpServletRequest request) throws Exception { if (file == null || file.isEmpty()) { return; } byte[] bytes = file.getBytes(); …………………… ……………… } @ExceptionHandler(Exception.class) public ModelAndView handleException(Exception ex,HttpServletRequest request) { Map<Object, Object> model = new HashMap<Object, Object>(); if (ex instanceof MaxUploadSizeExceededException){ model.put("errors", "The file should not be larger than"+ getFileKB(((MaxUploadSizeExceededException)ex).getMaxUploadSize())); } else{ model.put("errors", "I don't know the error: " + ex.getMessage()); } return new ModelAndView("/common/file/upload", (Map) model); } private String getFileKB(long byteFile){ if(byteFile==0) return "0KB"; long kb=1024; return ""+byteFile/kb+"KB"; }
- Step 3: Interface
<script type="text/javascript"> $(function() { $('#frmupload1').submit(function() { if ($('#file1').val() == '') { alert('Please choose to upload the import file!'); $('#file1').focus(); return false; }else{ if(!isvalidatefile($('#file1').val())) return false; } }); $('#frmupload2').submit(function() { if ($('#file2').val() == '') { alert('Please choose to upload the import file!'); $('#file2').focus(); return false; }else{ if(!isvalidatefile($('#file2').val())) return false; } }); }); function isvalidatefile(obj) { var extend = obj.substring(obj.lastIndexOf(".") + 1); //alert(extend); if (extend == "") { } else { if (!(extend == "xls" )) { alert("Please upload a file with the extension xls(Excel2003) or xlsx(Excel2007)!"); return false; } } return true; } <body> <h1>Upload file</h1> <form action="" method="post" enctype="multipart/form-data" onsubmit="return checkSubmit();"> <p>Please select a file:</p> <p ${not empty errors ?"style='color : red;'":""}>${errors}</p> <input type="file" name="file" id="file"/> <input type="submit" value="确定"/> </form> </body>
- Step 4: Receive the common parameters of the form form
The method is: after forcibly converting HttpServletRequest req into MultipartHttpServletRequest req, get it through req.getParameter("id");
HttpServletRequest request; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); String id = multipartRequest.getParameter("id"); String fileName = file.getOriginalFilename();
Reference: http://blog.csdn.net/z69183787/article/details/44152287
spring mvc file upload (single file upload | multiple file upload)
http://www.cnblogs.com/dengfukui/p/6252798.html