package org.springboot.controller;

 import org.springboot.constant.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder; /**
* @Auther:GongXingRui
* @Date:2018/12/24
* @Description:
**/
@Controller
public class FileDownload { //实现Spring Boot 的文件下载功能,映射网址为/download
@RequestMapping("/download")
public String downloadFile(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
// String picPath = "C:/Temp/pic";
String picPath = Constant.CONFIG_PROPERTIES.getProperty("download.path"); // 获取指定目录下的第一个文件
File scFileDir = new File(picPath);
File TrxFiles[] = scFileDir.listFiles();
System.out.println(TrxFiles[0]);
String fileName = TrxFiles[0].getName(); //下载的文件名 // 如果文件名不为空,则进行下载
if (fileName != null) {
//设置文件路径
File file = new File(picPath, fileName); // 如果文件名存在,则进行下载
if (file.exists()) {
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("Download the file successfully!");
}
catch (Exception e) {
System.out.println("Download the file failed!");
}
finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
} }

SpringBoot之文件下载的更多相关文章

  1. SpringBoot的文件下载

    SpringBoot的文件下载 2017年11月29日 10:32:20 阅读数:3907 SpringBoot的文件下载方法有很多,此处只记录使用Spring的Resource实现类FileSyst ...

  2. SpringBoot/SpringMVC文件下载方式

    本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...

  3. 用Springboot实现文件下载功能

    ApiOperation(value = "下载文件", httpMethod = "GET", notes = "downloadFile" ...

  4. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

  5. vue下载文件

    import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...

  6. SpringBoot(三):文件下载

    SpringBoot(三):文件下载 2017年08月02日 10:46:42 阅读数:6882 在原来的SpringBoot–uploadfile项目基础上添加文件下载的Controller: @R ...

  7. Springboot文件下载

    https://blog.csdn.net/stubbornness1219/article/details/72356632 Springboot对资源的描述提供了相应的接口,其主要实现类有Clas ...

  8. springBoot中使用使用junit测试文件上传,以及文件下载接口编写

    本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...

  9. 对Web(Springboot + Vue)实现文件下载功能的改进

    此为 软件开发与创新 课程的作业 对已有项目(非本人)阅读分析 找出软件尚存缺陷 改进其软件做二次开发 整理成一份博客 原项目简介 本篇博客所分析的项目来自于 ジ绯色月下ぎ--vue+axios+sp ...

随机推荐

  1. 007_Python中的__init__,__call__,__new__

    __init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...

  2. ROS 小乌龟测试

    教程 1.维基 http://wiki.ros.org/cn/ROS/Tutorials 2. 创客智造 http://www.ncnynl.com/category/ros-junior-tutor ...

  3. yaml的简单学习

    参考http://www.ruanyifeng.com/blog/2016/07/yaml.html 基本语法规则如下.    •    大小写敏感    •    使用缩进表示层级关系    •   ...

  4. 从String.valueOf(null)说起

    同学在群问String.valueOf(null)返回啥,我看了下源码,返回"null"啊,   public static String valueOf(Object obj) ...

  5. UIWindow 官方文档解析

    UIWindow定义了一个window对象,其用于管理和协调一个app在设备屏幕上的显示.除非一个app能在外部设备上显示内容,一般就只有一个window. window的主要功能:1)提供一个区域来 ...

  6. 一看就懂的Mybatis框架入门笔记

    本篇为初学Mybatis框架时的入门笔记,整理发出 Spring集成Mybatis https://www.cnblogs.com/yueshutong/p/9381590.html SpringBo ...

  7. Java基础之数据比较Integer、Short、int、short

    基础很重要,基础很重要,基础很重要.重要的事情说三遍,. 今天聊一聊Java的数据比较,这个范围比较大,基础类型的比较.引用类型的比较. 前提: 1.Java和c#都提供自动装箱和自动拆箱操作,何为自 ...

  8. RuntimeError: Python is not installed as a framework.

    RuntimeError: Python is not installed as a framework. 文章转载:https://www.cnblogs.com/harelion/p/563776 ...

  9. python之socket模块详解--小白博客

    主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...

  10. Python_查找员工信息-48

    ''' 查找出userinfo文件中年龄大于22岁的员工姓名和年龄 1,Alex,22,13651054608,IT 2,Egon,23,13304320533,Tearcher 3,nezha,25 ...