web.xml的基本配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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> <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.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--解决中文乱码--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
jsp页面如下代码 upload和download
upload: <form action="upload.do" method="post" enctype="multipart/form-data"> <%--用户名:<input name="username" > 密码:<input type="password" name="password">--%> 文件 :<input type="file" name="file"> <%--<input type="button" value="注册" >--%> <button>提交</button> </form> download: <a href="download.do?name=${path}">文件下载</a>
springMVC(springmvcxml)和spring(applicationContext.xml)的配置文件(spring的主配置文件不需要配置任何东西,这里我们只要创建就好了,不用管理):
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.aaa.springmvc.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value=""></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
控制器代码如下:
package com.aaa.springmvc.controller; import com.aaa.spring.file.util.FileUtil; import com.aaa.springmvc.entity.Users; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.UUID; @Controller public class UserControler { @RequestMapping("/upload") public String upload(Users users, MultipartFile file, Model model) { //真是文件名 String trueName = file.getOriginalFilename(); System.out.println(file.getName()); //后缀名 String suffix = trueName.substring(trueName.lastIndexOf(".")); String path = "d:/img/";
//使用UUID工具类为文件生成唯一文件名 String savename = UUID.randomUUID().toString(); File NewFile = new File(path + savename + suffix); try {
//写入文件的 file.transferTo(NewFile); } catch (IOException e) { e.printStackTrace(); } //存储value model.addAttribute("path", path + savename + suffix); return "login"; } @RequestMapping("/download") public String download(String name, HttpServletResponse resp) { resp.setContentType("application/octet-stream"); resp.setCharacterEncoding("UTF-8"); //设置文件的名字 resp.addHeader("Content-Disposition", "attachment; filename=" + name); InputStream is = null; OutputStream os = null; try { is = new FileInputStream(name); os = resp.getOutputStream(); byte[] b = new byte[2048 * 10]; int len = 0; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { //关闭文件流 FileUtil.close(is, os); } return "success"; } @RequestMapping("/login") public String login() { return "index"; } }