文件上传客户端三要素&原理
单文件上传
1 pom.xml 2 3 + 4 5 <!--fileupload和io坐标,用于文件上传--> 6 <dependency> 7 <groupId>commons-fileupload</groupId> 8 <artifactId>commons-fileupload</artifactId> 9 <version>1.3.1</version> 10 </dependency> 11 <dependency> 12 <groupId>commons-io</groupId> 13 <artifactId>commons-io</artifactId> 14 <version>2.3</version> 15 </dependency>
1 spring-mvc.xml 2 3 + 4 5 <!--配置文件上传解析器--> 6 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 7 <property name="defaultEncoding" value="UYF-8"/> 8 <property name="maxUploadSize" value="500000"/> 9 </bean>
1 upload.jsp 2 3 <h1>单文件上传</h1> 4 <form action="${pageContext.request.contextPath}/user/save22" method="post" enctype="multipart/form-data"> 5 名称<input type="text" name="username"><br/> 6 文件1<input type="file" name="uploadFile"><br/> 7 <input type="submit" value="提交"> 8 </form>
1 /** 2 * 单文件上传 3 * @param username 4 * @param uploadFile 5 */ 6 @RequestMapping("/save22") 7 @ResponseBody 8 public void save22(String username, MultipartFile uploadFile) throws IOException { 9 System.out.println(username); //zhangsan 10 System.out.println(uploadFile); //org.springframework.web.multipart.commons.CommonsMultipartFile@308dbb28 11 12 //获取上传文件的名称 13 String originalFilename = uploadFile.getOriginalFilename(); 14 //存储到目标地点 15 uploadFile.transferTo(new File("E:\\upload\\" + originalFilename)); 16 }
多文件上传
1 /** 2 * 多文件上传 3 * @param username 4 * @param uploadFile 5 * @param uploadFile2 6 * @throws IOException 7 */ 8 @RequestMapping("/save23") 9 @ResponseBody 10 public void save23(String username, MultipartFile uploadFile, MultipartFile uploadFile2) throws IOException { 11 System.out.println(username); 12 13 String originalFilename = uploadFile.getOriginalFilename(); 14 uploadFile.transferTo(new File("E:\\upload\\" + originalFilename)); 15 16 String originalFilename2 = uploadFile2.getOriginalFilename(); 17 uploadFile2.transferTo(new File("E:\\upload\\" + originalFilename2)); 18 } 19 20 21 /** 22 * 多文件上传(方式2) 23 * @param username 24 * @param uploadFile 25 * @throws IOException 26 */ 27 @RequestMapping("/save24") 28 @ResponseBody 29 public void save24(String username, MultipartFile[] uploadFile) throws IOException { 30 System.out.println(username); 31 for (MultipartFile file : uploadFile) { 32 String originalFilename = file.getOriginalFilename(); 33 file.transferTo(new File("E:\\upload\\" + originalFilename)); 34 } 35 }
1 upload.jsp 2 3 <h1>单文件上传</h1> 4 <form action="${pageContext.request.contextPath}/user/save23" method="post" enctype="multipart/form-data"> 5 名称<input type="text" name="username"><br/> 6 文件1<input type="file" name="uploadFile"><br/> 7 文件2<input type="file" name="uploadFile2"><br/> 8 <input type="submit" value="提交"> 9 </form> 10 11 <hr> 12 13 <h1>单文件上传(方式2)</h1> 14 <form action="${pageContext.request.contextPath}/user/save24" method="post" enctype="multipart/form-data"> 15 名称<input type="text" name="username"><br/> 16 文件1<input type="file" name="uploadFile"><br/> 17 文件2<input type="file" name="uploadFile"><br/> 18 文件2<input type="file" name="uploadFile"><br/> 19 <input type="submit" value="提交"> 20 </form>
方式1测试
方式2(推荐)测试
SSM4知识总结