文件上传
导入依赖
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
在配置文件中配置文件解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize" value="10485760"/> </bean>
后端控制器
@Controller public class HelloController { @RequestMapping(path = "/upload") public String uploadTest(HttpServletRequest request, MultipartFile upload) throws Exception { { System.out.println("SpringMVC方式的文件上传..."); // 先获取到要上传的文件目录 String path = request.getSession().getServletContext().getRealPath("/uploads"); // 创建File对象,一会向该路径下上传文件 File file = new File(path); // 判断路径是否存在,如果不存在,创建该路径 if (!file.exists()) { file.mkdirs(); } // 获取到上传文件的名称 String filename = upload.getOriginalFilename(); String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(); //把文件的名称唯一化 filename = uuid + "_" + filename; // 上传文件 upload.transferTo(new File(file, filename)); return "success"; } } }
跨服务器上传
......