SpringMVC上传下载依赖组件包:
SpringMVC上传:
fileupload.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>Insert title here</title> </head> <body> <form action="fileupload" enctype="multipart/form-data" method="post"> 文件:<input type="file" name="file"><br/> <input type="submit" value="提交"> </form> </body> </html>
springmvc.xml中MultipartResovler 组件配置:
<?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/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"> <!-- 扫描注解 负责controller层 注解扫描 --> <context:component-scan base-package="com.tao.controller"></context:component-scan> <!-- 设置注解驱动,注册HandlerMapping和HandlerAdapter --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 设置静态资源 --> <mvc:resources location="/js/" mapping="/js/**"></mvc:resources> <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> <mvc:resources location="/images/" mapping="/images/**"></mvc:resources> <mvc:resources location="/files/" mapping="/files/**"></mvc:resources> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- MultipartResovler 解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设定默认编码 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件的最大值为5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> <!-- 设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240 --> <property name="maxInMemorySize" value="40960"></property> </bean> </beans>
FileUploadController:
package com.tao.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; 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; /** * SpringMVC文件上传 * @author zhangtao * */ @Controller public class FileUpLoadController { @RequestMapping("/fileupload") public ModelAndView upload(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request,HttpSession session){ if(!file.isEmpty()){ //文件存放路径 String path = request.getServletContext().getRealPath("/")+"uploadFileDir\\"; System.out.println(path); //文件名称 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String name = String.valueOf(format.format(new Date())+"_"+file.getOriginalFilename()); System.out.println(name); File destFile = new File(path,name); //转存文件 try { file.transferTo(destFile); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } //访问的url String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"+name; } ModelAndView mv = new ModelAndView(); mv.setViewName("success"); return mv; } }
SpringMVC下载:
download.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>Insert title here</title> </head> <body> <a href="download?fileName=a.txt">下载</a> </body> </html>
downloadController:
package com.tao.controller; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * SpringMVC文件下载 * @author zhangtao * */ @Controller public class DownloadController { @RequestMapping("download") public void download(String fileName,HttpServletRequest request,HttpServletResponse response) throws Exception{ //PrintWriter out = response.getWriter(); //字符流 response.setHeader("Content-Disposition", "attachment;filename="+fileName); //设置下载响应头 ServletOutputStream outputStream = response.getOutputStream(); //以二进制文件流响应 String filePath = request.getServletContext().getRealPath("files"); //得到当前工程files文件加的资源路径 File file = new File(filePath,fileName); byte[] downloadByte = FileUtils.readFileToByteArray(file); outputStream.write(downloadByte); outputStream.flush(); outputStream.close(); } }