FileUtils

下载jar中的文件

package com.meeno.chemical.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder; /**
* 文件工具类
*/
@Slf4j
public class FileUtils { /**
* 下载指定目录文件
* @param res
* @param downFileName 下载文件名 xxx模板.xlsx
* @param resourcePath 下载路径 static/down/materielTemplate.xlsx
*/
public static void downloadFile(HttpServletResponse res,String downFileName,String resourcePath){
String fileName = downFileName; res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream;charset=utf-8");
try {
res.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] buff = new byte[1024];
// BufferedInputStream bis = null;
InputStream bis = null;
OutputStream os = null;
try { //第一种
//ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx");
//InputStream inputStream =classPathResource.getInputStream(); //第二种
//InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx"); //第三种
//InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx"); //第四种
//File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx");
//InputStream inputStream = new FileInputStream(file); //前三种方法在开发环境(IDE中)和生产环境(linux部署成jar包)都可以读取到,第四种只有开发环境 时可以读取到,生产环境读取失败。
//推测主要原因是springboot内置tomcat,打包后是一个jar包,无法直接读取jar包中的文件,读取只能通过类加载器读取。
//前三种都可以读取到其实殊途同归,直接查看底层代码都是通过类加载器读取文件流,类加载器可以读取jar包中的编译后的class文件,当然也是可以读取jar包中的excle模板了。 //String path = ResourceUtils.getURL("classpath:static/projectRes/樊登.xlsx").getPath();
//Resource resource = new ClassPathResource("static/projectRes/樊登子级用户模板.xlsx");
bis = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
//Resource resource = new ClassPathResource(resourcePath);
//File file = resource.getFile();
os = res.getOutputStream();
//bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff); while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

FileUtils 文件工具类的更多相关文章

  1. java FileUtils 文件工具类

    package com.sicdt.library.core.utils; import java.io.BufferedInputStream; import java.io.File; impor ...

  2. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  3. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

  4. java文件工具类

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  5. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  6. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  7. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  8. Java 实现删除文件工具类

    工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...

  9. Android FileUtils 文件操作类

    系统路径 Context.getPackageName(); // 用于获取APP的所在包目录 Context.getPackageCodePath(); //来获得当前应用程序对应的apk文件的路径 ...

随机推荐

  1. 【Spring】Spring中的循环依赖及解决

    什么是循环依赖? 就是A对象依赖了B对象,B对象依赖了A对象. 比如: // A依赖了B class A{ public B b; } // B依赖了A class B{ public A a; } ...

  2. QT从入门到入土(三)——信号和槽机制

    摘要 信号槽是 Qt 框架引以为豪的机制之一.所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号 (signal).这种发出是没有目的的,类似广播 ...

  3. Min25 筛学习笔记

    仅仅是 \(min25\) 筛最基本的方法,没有任何推式子的例题.(想了想还是加两道吧qwq) 这里解决的是 \(Luogu\) 那道模板题. min25 基本方法: 最基础的是两个式子: \[G(n ...

  4. 必须要了解的Linux基本操作

    Linux常用的基础操作             1.命令行提示字符             2.切换用户             3.查看当前主机的完整名称             4.临时设置主机 ...

  5. C语言:变量

    变量: 1.在程序运行过程中,值可以改变的量称为变量 2.每个变量都有一个名字,称为变量名 3.每个变量都必须进行变量说明,指明变量的类型 4.每个变量都有一个对应的地址,写法:&变量名 5. ...

  6. C语言:fopen函数

    在C语言中,操作文件之前必须先打开文件:所谓"打开文件",就是让程序和文件建立连接的过程.打开文件之后,程序可以得到文件的相关信息,例如大小.类型.权限.创建者.更新时间等.在后续 ...

  7. SpringBoot配置Https

    HTTPS (全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认 ...

  8. 算法基础~链表~排序链表的合并(k条)

    算法基础~链表~排序链表的合并(k条) 1,题意:已知k个已排序链表头结点指针,将这k个链表合并,合并后仍然为有序的,返回合并后的头结点. 2,方法之间时间复杂度的比较: 方法1(借助工具vector ...

  9. Leetcode:637. 二叉树的层平均值

    Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...

  10. [考试总结]noip模拟13

    因为最近考试频繁,所以咕掉了好长时间... 淦,刚说完又来一场... 先咕了,等以后有时间再写.... 回来了... 首先看到这个题目们,感觉就不存好意... 然后开始开 \(T1\). 只能蒻蒻地按 ...