spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationContext.xml中 <bean id=”multipartResolver”
class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/>
关于文件上传的配置不能少
大家可以看具体代码如下:
注意:要导入的依赖包:
com.springsource.org.apache.commons.io-1.4.0.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 文件上传 --> <!-- 1.在web.xml中添加listener 2.在web.xml中添加spring框架启动的加载的配置文件路径: --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value> </context-param> </web-app>
applicationContext.xml
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/views/jsp/" p:suffix=".jsp"> </bean> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
注意:要添加前后缀,必须在XML文件上配置
xmlns:p="http://www.springframework.org/schema/p"
Controller.java
@RequestMapping(value = "/upload.do") public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { System.out.println("开始"); String path = request.getSession().getServletContext().getRealPath("upload"); System.out.println(path); String fileName = file.getOriginalFilename(); // String fileName = new Date().getTime()+".jpg"; System.out.println(path); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName); return "experiment/kit"; }
index.jsp
<form enctype="multipart/form-data" action="${paths}/upload.do" method="post">
<p style="font-size:16px;">请选择正确的excel文件上传</p> <input id="txt" type="text" disabled="disabled" value="文件域" name="txt"> <input type="button" onclick="file.click()" size="30" value="上传文件" onmousemove="file.style.pixelLeft=event.x-60;file.style.pixelTop=this.offsetTop;"> <input id="file1" class="files" type="file" hidefocus="" size="1" style="height:26px;" name="file" onchange="txt.value=this.value"> <p style="color:red;">支持的excel格式为:xls、xlsx、xlsb、xlsm、xlst!</p> <input class="btn btn_ok" type="submit" value="确认"> </form>