放上代码。
1、springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.itheima.comtroller"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="10000000"/> </bean>2、file.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>文件上传</title> <script> function check() { var name = document.getElementById("name").value; var file = document.getElementById("file").value; if(name==""){ alert("请选择上传人"); return false; } if(file==""||file.length==0){ alert("请选择上传的文件"); return false; } return true; } </script> </head> <body> <form action="${pageContext.request.contextPath}/fileUpload" method="post" enctype="multipart/form-data" onsubmit="return check()"> 上传人:<input id="name" type="text" name="name"><br/> 请选择文件:<input id="file" type="file" name="uploadfile" multiple="multiple"><br/> <input type="submit" value="上传"> </form> </body> </html>3、FileUploadController.java
package com.itheima.comtroller; import java.io.File; import java.util.List; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { @RequestMapping(value="/fileUpload") public String handleFormUpload(@RequestParam("name") String name, @RequestParam("uploadfile") List<MultipartFile> uploadfile, HttpServletRequest request) { if(!uploadfile.isEmpty() && uploadfile.size()>0) { for(MultipartFile file : uploadfile) { String originalFilename = file.getOriginalFilename(); String dirPath = request.getServletContext().getRealPath("/upload/"); File filePath = new File(dirPath); if (!filePath.exists()) { filePath.mkdirs(); } String newFilename = name+"_"+UUID.randomUUID()+"_"+originalFilename; try { file.transferTo(new File(dirPath + newFilename)); } catch (Exception e) { e.printStackTrace(); return "error"; } }System.out.println("1111"); return "success"; } else { return "error"; } } }4、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringMVCJSON</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>5、项目路径
6、jar包都有导入 7、问题 在选择完上传人和上传文件之后会报错404,应该是没有进入Controller里面,可是我找不到哪里出了问题。
正在学习SSM中,这个问题困扰我一晚上了。
问题解决了: 把Controller和jsp中的fileUpload后面都加了个1就好了,不知为啥。