FileUtils 文件工具类
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 文件工具类的更多相关文章
- java FileUtils 文件工具类
package com.sicdt.library.core.utils; import java.io.BufferedInputStream; import java.io.File; impor ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- Java常用工具类---IP工具类、File文件工具类
package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...
- java文件工具类
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 自动扫描FTP文件工具类 ScanFtp.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...
- Java 实现删除文件工具类
工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...
- Android FileUtils 文件操作类
系统路径 Context.getPackageName(); // 用于获取APP的所在包目录 Context.getPackageCodePath(); //来获得当前应用程序对应的apk文件的路径 ...
随机推荐
- Pandas高级教程之:window操作
目录 简介 滚动窗口 Center window Weighted window 加权窗口 扩展窗口 指数加权窗口 简介 在数据统计中,经常需要进行一些范围操作,这些范围我们可以称之为一个window ...
- 自动化测试 如何快速提取Json数据
Json作为一种轻量级的交换数据形式,由于其自身的一些优良特性比如包含有效信息多,易于阅读和解析. 使用Json的场景也很多,比如读取解析系列化的Json格式的数据,我们需要将一个Json的字符串解析 ...
- C语言:变量
变量: 1.在程序运行过程中,值可以改变的量称为变量 2.每个变量都有一个名字,称为变量名 3.每个变量都必须进行变量说明,指明变量的类型 4.每个变量都有一个对应的地址,写法:&变量名 5. ...
- urllib库中的URL编码解码和GETPOST请求
在urllib库的使用过程中,会在请求发送之前按照发送请求的方式进行编码处理,来使得传递的参数更加的安全,也更加符合模拟浏览器发送请求的形式.这就需要用urllib中的parse模块.parse的使用 ...
- 每天五分钟Go - Map
map的定义 var m map[type]type fmt.Println(m) 此种方法定义的m为nil //打印的结果为: map[] map的创建 1.使用make创建 var m1 = ma ...
- 【贪心】数列分段Section I luogu-1181
题目描述 对于给定的一个长度为\(N\)的正整数数列\(A_i\),现要将其分成连续的若干段,并且每段和不超过\(M\)(可以等于\(M\)),问最少能将其分成多少段使得满足要求. 分析 简单思考一下 ...
- Lniux上的tomcat部署web项目路径问题
一.项目路径及部署.启动位置 1.在tomcat下部署应用,部署路径:一般直接部署在tomcat/webapps/ROOT下即可.2.默认访问路径:tomcat的默认访问路径为webapps/ROOT ...
- SAML 2.0简介(1)
1.什么是SAML: SAML是Web浏览器用来通过安全令牌启用单点登录(SSO)的标准协议 2.优点: 跨多个应用程序管理用户身份和授权. 3.单点登录(SSO)是什么: 它使用户仅使用一组凭据(用 ...
- python批量修改图片名称
import os class BatchRename(): def rename(self): # windows环境 """ os.rename() 方法用于命名文件 ...
- glassfish Client not authorized for this invocation.解决办法
javax.ejb.EJBAccessException at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContain ...