今天在进行文件上传逻辑的编写
在jsp页面加上了enctype="multipart/form-data"
1 <form action="doUpload" method="post" enctype="multipart/form-data"> 2 <div class="form-group fg1" style="margin-top: 30px; "> 3 <input type="file" name="video" value="上传视频" style="position:absolute;opacity: 0;"> 4 <span>选择上传视频 <i class="icon-download-alt icon-2x"></i></span> 5 </div> 6 7 <div class="form-group"> 8 <input type="text" placeholder="视频标题" name="title"> 9 </div> 10 <div class="form-group"> 11 <input type="text" placeholder="给你的视频一点表述吧" name="desc"> 12 </div> 13 <div class="form-group"> 14 <select name="type" id="category_name" > 15 <option value="">选择类型</option> 16 <option value="animation">动漫</option> 17 <option value="dance">舞蹈</option> 18 <option value="game">游戏</option> 19 <option value="ghost">鬼畜</option> 20 <option value="movie">电影</option> 21 </select> 22 </div> 23 24 <div class="form-group"> 25 <input type="submit" value="投稿"> 26 </div> 27 28 </form>
在controller里,写了@RequestParam("video") MultipartFile file
1 @PostMapping("doUpload") 2 public String doUpload(HttpSession session, HttpServletRequest request, @RequestParam("video") MultipartFile file){ 3 //, @RequestParam("video") MultipartFile file, Video video, String title, String desc, String category_name 4 Logger logger = LoggerFactory.getLogger(UserController.class); 5 String fileUrl = file.getOriginalFilename(); 6 Video media = new Video(); 7 media.setName(request.getParameter("title")); 8 media.setDesc(request.getParameter("desc")); 9 logger.info("视频名称为:" + media.getName() + " 视频描述为:" + media.getDesc() + " 视频类型为:" + request.getParameter("type")); 10 logger.info(fileUrl); 11 return "upload";
在springmvc中,写了bean标签
1 <bean id="multipartResolver" 2 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <!-- 设定默认编码 --> 4 <property name="defaultEncoding" value="UTF-8"></property> 5 <!-- 设定文件上传的最大值5MB,5*1024*1024 --> 6 <property name="maxUploadSize" value="99999999"></property> 7 </bean>
可是!!!仍然报错
后来发现!
居然是pom文件内没有导入所需要的包!!!!
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <!-- Apache Commons IO --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>
一定要导入啊啊啊啊!!!