zbb20180613 Spring MVC实现大文件下载功能
<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>
|
<?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>
|
<?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>
|
<?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>
|
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级别的,若一次性从文件流读取数据如:
- 接下去就是响应客户端的请求了,这里就不用多说了吧。
转载于:https://www.cnblogs.com/super-admin/p/9177800.html
zbb20180613 Spring MVC实现大文件下载功能相关推荐
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody@RequestMapping("/logout")public Json logout(HttpSession session,H ...
- 5000 字的 Spring MVC 全面大总结
概述 SpringMVC再熟悉不过的框架了,因为现在最火的SpringBoot的内置MVC框架就是SpringMVC.我写这篇文章的动机是想通过回顾总结一下,重新认识SpringMVC,所谓温故而知新 ...
- Spring Boot整合批量文件下载功能
导语 在测试功能中,出现了再Spring Boot中打包压缩文件的需求,下面来实现一个这个操作 /*** 批量下载文件** @param request* @param response*/@Ge ...
- Spring MVC 特性实现文件下载
代码 import java.io.File; import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtil ...
- 编写 Spring MVC 控制器的 14 个技巧
欢迎关注方志朋的博客,回复"666"获面试宝典 通常,在Spring MVC中,我们编写一个控制器类来处理来自客户端的请求.然后,控制器调用业务类来处理与业务相关的任务,然后将客户 ...
- 分享 14 个 Spring MVC 顶级技巧!
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 通常,在Spring MVC中,我们编写一个控制器类来处理来自客户 ...
- Spring 2.5 基于注解驱动的 Spring MVC
基于注解的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,为 Spring MVC 提供了完全基于注解的配置.本文将介绍 Spring 2.5 新增的 Sping MVC 注解功能,讲述如 ...
- 一步一步手绘Spring MVC运行时序图(Spring MVC原理)
相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...
- 一步一步手绘Spring MVC运行时序图
Spring MVC 初体验 初探Spring MVC 请求处理流程 Spring MVC 相对于前面的章节算是比较简单的,我们首先引用<Spring in Action>上 的一张图来了 ...
最新文章
- Linux内核分析:完成一个简单的时间片轮转多道程序内核代码
- docker进阶-搭建私有企业级镜像仓库Harbor
- 【温故知新】CSS学习笔记(字体样式属性)附加篇
- python开发编译器_python开发编译器
- CodeForces 7D	 Palindrome Degree 字符串hash
- SpringCloud Sentinel 使用restTemplate的两种配置介绍
- 【logstash】使用logstash拉取数据到kerberos+SSL认证的kafka集群中遇到的坑
- Android手机安全性测试手段
- DB2数据库对现有表格字段修改
- phpMyAdmin view_create.php 跨站脚本漏洞
- REDIS学习总结(一)单机集群搭建
- 在Solaris下安装中文语言包
- ubuntu 添加证书
- java 排班日历,排班日历
- RGB-D SLAM in Dynamic Environments Using Static Point Weighting论文笔记
- win10 提升administrator权限 管理员权限
- python最强脚本工具_python脚本工具最百里自瞄
- 交换机配置软件具有的作用
- CTFHub | Refer注入
- 最大不相交子集 POJ1328
热门文章
- JavaScript文件存储信息对象cookie编码生存期
- 如何取消重要地点加密_西门子S7-300/400PLC编程之软件加密
- 全栈性能测试修炼宝典jmeter实战电子版_推荐一款技术人必备的接口测试神器:Apifox...
- python抓取图片_Python3简单爬虫抓取网页图片
- php微信转跳浏览器代码,通用微信QQ跳转浏览器打开代码
- python数据驱动测试设计_GitHub - 13691579846/DataDriverTestFrameWork: python+selenium+pageobject数据驱动测试框架...
- pip环境变量配置 python3.6_零基础学python之爬虫第一节环境配置第一章:Python3+Pip环境配置...
- LeetCode解题思路—双指针模式
- 在inet下写一个“HelloWorld”程序
- LuaForUnity1:Lua介绍与使用