1. 首先先配置web.xml:
<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"> <!-- <welcome-file-list>--> <!-- <welcome-file>index.jsp</welcome-file>--> <!-- </welcome-file-list>--> <servlet> <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>fileencoding</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>fileencoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.创建springmvc.xml并配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--自动扫描包--> <context:component-scan base-package="zpy.controller"></context:component-scan> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <!--注入视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀和后缀--> <!--把页面放在什么地方--> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10240000"/> </bean> </beans>
3. index.jsp页面:写的多个上传,不需要的话就把另外两个文件上传删了,另外等会的Contorller页面里的for循环以及里面的[i]删掉即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上传</title> </head> <body> <form action="api/upload" method="post" enctype="multipart/form-data"> 文件上传 <input type="file" name="file"><br/> 文件上传 <input type="file" name="file"><br/> 文件上传 <input type="file" name="file"><br/> <input type="submit" value="上传"> <a href="api/download?fileName=zpy.jpg">下载</a> </form> </body> </html>
4. Controller页面代码: 上面是上传下面的是下载
package zpy.controller; import org.apache.commons.io.FileUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; 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; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Random; @Controller @RequestMapping("/api") public class HiContorller { @RequestMapping("/upload") public String Uploading(MultipartFile[] file, HttpServletRequest request) { for (int i = 0; i < file.length; i++) { //获取上传的路经 String path = request.getServletContext().getRealPath("/upload/"); //拿到页面上传过来的文件 String name = file[i].getOriginalFilename(); //拿到文件名改名 String newName = new Date().getTime() + new Random(9999999).nextInt() + name; //上传的路径 File f = new File(path + newName); System.out.println(f); // System.out.println(path+newName); //上传 try { file[i].transferTo(f); } catch (IOException e) { e.printStackTrace(); } } return "show"; } @RequestMapping("/download") public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName, HttpServletRequest request) throws IOException { //下载的路径 String path = request.getServletContext().getRealPath("/download/"); //下载的完整路径 File f = new File(path + fileName); //转格式 String name = new String(fileName.getBytes("utf-8"), "iso-8859-1"); HttpHeaders hh = new HttpHeaders(); hh.setContentDispositionFormData("attachment", name); hh.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED); } }
5. 注意要创建俩个文件夹以及idea的webapp下面也要创建相同的文件夹:
分别是:上传 : download
下载 : upload
另外需要注意的是要在tomcat的文件夹里去创建这两个文件夹
以上就是上传下载的全部内容了,希望能帮助到大家。