zbb20180613 Spring MVC实现大文件下载功能

Spring MVC实现大文件下载功能
TestUploadFile.zip
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zbb</groupId>
<artifactId>TestUploadFile</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestUploadFile Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
 
 
</dependencies>
<build>
<finalName>TestUploadFile</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
 
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_3_1.xsd"
version="3.1">
 
<!-- *** 初始化Spring容器开始 *** -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- === 初始化Spring容器结束 === -->
 
<!-- *** 初始化SpringMVC开始 *** -->
<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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- === 初始化SpringMVC结束 === -->
</web-app>
spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 
<context:component-scan base-package="com.zbb" />
</beans>
spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
<mvc:annotation-driven />
 
<mvc:default-servlet-handler />
 
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
 
<!--只扫描@Controller注解类,否则影响事务 -->
<context:component-scan base-package="com.zbb"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
 
 
TestDownLoadFileController .java
package com.zbb.controller;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping("/uploadFile")
public class TestDownLoadFileController {
@RequestMapping("/download")
public void downloadNet(HttpServletResponse response) throws Exception {
try {
String path = "E:\\BaiduYunDownload\\黑马程序员视频.z03";
File file = new File(path);
if (file.exists()) {
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file));
response.reset();
response.setContentType("application/x-download");
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(filename.getBytes(), "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream out = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
 
byte[] buffer = new byte[1024 * 1024 * 10];
int i = -1;
while ((i = fis.read(buffer)) != -1) {
out.write(buffer, 0, i);
 
}
fis.close();
out.flush();
out.close();
try {
response.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
PrintWriter out = response.getWriter();
out.print("<script>");
out.print("alert(\"not find the file\")");
out.print("</script>");
}
} catch (IOException ex) {
PrintWriter out = response.getWriter();
out.print("<script>");
out.print("alert(\"not find the file\")");
out.print("</script>");
}
}
}
 
 
实现文件下载主要主要步骤如下
  • 创建文件对象:File file = new File(path);
  • 利用BufferedInputStream从文件流读取数据:这里选用BufferedInputStream而不是InputStream,是因为在读取大文件时,BufferedInputStream的速度快上很多;
  • 设置reponsed对象,响应客户端的请求,即下载文件。
  • 这里需要注意的是,由于一般的视频文件都是G级别的,若一次性从文件流读取数据如:
byte[] buffer = new byte[fis. available()]
会导致内存溢出,所以不能一次性读取,应每次读取一部分,如每次读取10M:
     byte[] buffer = new byte[1024 * 1024 * 10];
  •  接下去就是响应客户端的请求了,这里就不用多说了吧。
  在jsp页面只需设置一个链接指向该action即可
      <a href="/download" >下载</a>
  页面点击链接即可下载该视频:
 
posted @ 2018-06-13 14:54  DaryllD  阅读(291)  评论(0编辑  收藏  举报