jsp
<form action="/upload" name="Form" id="Form" method="post" enctype="multipart/form-data">
xml
<!-- 上传拦截,如最大上传值及最小上传值 1024为 1byte --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> <property name="defaultEncoding"> <value>utf-8</value> </property> </bean>
controller 里 MultipartFile file 必须加 @RequestParam
@RequestMapping(value="/upload") public ModerAndView save( @RequestParam(required=false) MultipartFile file ) throws Exception{ //... copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "") //... return mv }
-----上传 File.separator 路径自适应分割符
/** * 写文件到当前目录的upload目录中 * * @param in * @param fileName * @throws IOException */ private static String copyFile(InputStream in, String dir, String realName) throws IOException { File file = new File(dir, realName); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } FileUtils.copyInputStreamToFile(in, file); return realName; }