package com.bsh.server.utils;
import com.bsh.server.pojo.ResultCodeEnum;
import com.bsh.server.pojo.ResultVo;
import com.bsh.server.service.IReportfileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@Slf4j
public class FileUtil {
// 文件上传路径
private static final String FILE_UPLOAD_PATH = "upload" + File.separator;
// 文件下载路径
private static final String FILE_DOWNLOAD_PATH = "upload" + File.separator;
// 日期路径
private static final String DATE_PATH = DateUtil.getNowStr() + File.separator;
// 根路径
private static final String ROOT_PATH = "E:" + File.separator;
// 下划线
private static final String UNDER_LINE = "_";
// 默认字符集
private static final String DEFAULT_CHARSET = "utf-8";
private static final String filePath = ROOT_PATH + FILE_UPLOAD_PATH + DATE_PATH;
// 上传文件
public static MultipartFile uploadFile(MultipartFile file) throws IOException {
IReportfileService reportfileService;
// 获取上传的文件名称(包含后缀名)
String oldFileName = file.getOriginalFilename();
// 获取文件后缀名,将小数点“.” 进行转译
String[] split = oldFileName.split("\\.");
// 文件名
String fileName = null;
StringBuilder builder = new StringBuilder();
if (split.length > 0) {
String suffix = split[split.length - 1];
for (int i = 0; i < split.length -1; i++) {
builder.append(split[i]).append(UNDER_LINE);
}
// 防止文件名重复
fileName = builder.append(System.nanoTime()).append(".").append(suffix).toString();
} else {
fileName = builder.append(oldFileName).append(UNDER_LINE).append(System.nanoTime()).toString();
}
// 上传文件的存储路径
// 生成文件夹
mkdirs(filePath);
// 文件全路径
String fileFullPath = filePath + fileName;
log.info("上传的文件:" + file.getName() + "," + file.getContentType() + ",保存的路径为:" + fileFullPath);
// 转存文件
Streams.copy(file.getInputStream(), new FileOutputStream(fileFullPath), true);
return file;
}
public String getFullPath(String fileName){
String fileFullPath = null;
fileFullPath = filePath + fileName;
return fileFullPath;
}
// 根据文件名下载文件
public static ResultVo<String> downloadFile(String fileName, HttpServletResponse response) {
System.out.println(fileName);
System.out.println(1);
InputStream in = null;
OutputStream out = null;
try {
// 获取输出流
out = response.getOutputStream();
setResponse(fileName, response);
System.out.println(2);
String downloadPath = new StringBuilder().append(ROOT_PATH).append(FILE_DOWNLOAD_PATH).append(DATE_PATH).append(fileName).toString();
File file = new File(downloadPath);
System.out.println(downloadPath);
if (!file.exists()) {
log.error("下载附件失败,请检查文件" + downloadPath + "是否存在");
return ResultUtil.error("下载附件失败,请检查文件" + downloadPath + "是否存在");
}
// 获取输入流
in = new FileInputStream(file);
if (null == in) {
log.error("下载附件失败,请检查文件" + fileName + "是否存在");
throw new FileNotFoundException("下载附件失败,请检查文件" + fileName + "是否存在");
}
// 复制
IOUtils.copy(in, response.getOutputStream());
response.getOutputStream().flush();
try {
close(in, out);
} catch (IOException e) {
log.error("关闭流失败");
return ResultUtil.error(ResultCodeEnum.CLOSE_FAILD.getCode(), "关闭流失败");
}
} catch (IOException e) {
log.error("响应对象response获取输出流错误");
return ResultUtil.error("响应对象response获取输出流错误");
}
return ResultUtil.success("文件下载成功");
}
// 设置响应头
public static void setResponse(String fileName, HttpServletResponse response) {
// 清空输出流
response.reset();
response.setContentType("application/x-download;charset=GBK");
try {
System.out.println(fileName);
response.setHeader("content-type","application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( (String)fileName, "UTF-8"));
// response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(DEFAULT_CHARSET), "UTF-8"));
} catch (UnsupportedEncodingException e) {
log.error("文件名{}不支持转换为字符集{}", fileName, DEFAULT_CHARSET);
}
}
// 关闭流
public static void close(InputStream in, OutputStream out) throws IOException{
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
// 根据目录路径生成文件夹
public static void mkdirs(String path) {
File file = new File(path);
if(!file.exists() || !file.isDirectory()) {
file.mkdirs();
}
}
}