Commit 2eb5a9b2 authored by wangyangyang's avatar wangyangyang

添加文件工具类

parent de1267a6
package com.yonde.dcs.plan.core.util;
import cn.hutool.core.io.IoUtil;
import com.yonde.dex.basedata.exception.DxBusinessException;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author xfchai
* @ClassName FileUtils.java
* @Description 文件工具类
* @createTime 2022/05/31 14:13:00
*/
@Slf4j
public class FileUtils {
public static void main(String[] args) {
String contentType = "application/vnd.ms-excel;charset=utf-8";
String fileName = "定额工时模板.xlsx";
}
/**
* 导出通用文件
*
* @param response
* @param fileInputStream
* @throws IOException
*/
public static void exportFile(HttpServletResponse response, FileInputStream fileInputStream, String exportType, String name) throws IOException {
//放到缓冲流里面
BufferedInputStream bins = new BufferedInputStream(fileInputStream);
//获取文件输出IO流
OutputStream outs = response.getOutputStream();
BufferedOutputStream bouts = new BufferedOutputStream(outs);
response.setContentType(exportType);
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-disposition", "attachment;filename=" + name);
//修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = fileInputStream.read(b)) != -1) {
out.write(b, 0, len);
}
//TODO 设置Content-Length大小
response.setHeader("Content-Length", String.valueOf(fileInputStream.getChannel().size()));
int bytesRead = 0;
byte[] buffer = new byte[8192];
//开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
//调用flush()方法
bouts.flush();
fileInputStream.close();
bins.close();
outs.close();
bouts.close();
out.close();
}
/**
* 删除目录下的所有文件
*
* @param file
* @param deleteDir 是否删除最外层目录
*/
public static void deleteDir(File file, Boolean deleteDir) {
//判断文件不为null或文件目录存在
if (file == null || !file.exists()) {
System.out.println("文件删除失败,请检查文件路径是否正确");
return;
}
//取得这个目录下的所有子文件对象
File[] files = file.listFiles();
//遍历该目录下的文件对象
for (File f : files) {
//打印文件名
String name = file.getName();
System.out.println(name);
//判断子目录是否存在子目录,如果是文件则删除
if (f.isDirectory()) {
deleteDir(f, true);
} else {
f.delete();
}
}
//删除空文件夹 for循环已经把上一层节点的目录清空。
if (deleteDir) {
file.delete();
}
}
/**
* 保存到本地目录
*
* @param inputStream
* @param fileUrl 文件绝对路径
*/
public static void inputToFile(InputStream inputStream, String fileUrl) {
OutputStream outputStream = null;
try {
try {
outputStream = new FileOutputStream(fileUrl);
} catch (FileNotFoundException e) {
log.info("保存到本地临时目录失败:{}!", fileUrl);
throw new DxBusinessException("500", "保存文件到本地临时目录失败:" + fileUrl);
}
IoUtil.copy(inputStream, outputStream);
} finally {
IoUtil.close(inputStream);
IoUtil.close(outputStream);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment