pom.xml 添加
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency>
applicationContext.xml 添加
<!--配置文件解析器对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean>
Controller实现
@RequestMapping(value = "upload",method = RequestMethod.POST) public String uploadFile(HttpServletRequest request, MultipartFile upload){ try { String path = request.getSession().getServletContext().getRealPath("/uploads/"); //判断该路径是否存在 File file = new File(path); if (!file.exists()) { file.mkdirs(); } //上传文件项 String filename = upload.getOriginalFilename(); String saveName = filename.substring(filename.lastIndexOf(File.separator) + 1); upload.transferTo(new File(path, saveName)); //Thread t = new LogToSqlThread(fileName,uploadPath,savePath); //t.start(); return "upload success"; } catch(Exception e) { return e.toString(); } }