package pit.common.utils.file;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import pit.common.exception.ExceptionCast;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
public class FileUtils {
public static String saveFile(MultipartFile file, String roothttpPath) {
String filename = file.getOriginalFilename();
String extName = FileUtil.extName(filename);
try {
String fileMd5 = DigestUtils.md5Hex(file.getBytes());
String filePath = roothttpPath + "/" + generateFilePath(fileMd5, extName);
File targetFile = new File(filePath);
if (targetFile.exists()) {
return filePath;
}
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
file.transferTo(targetFile);
return filePath;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String generateFilePath(String fileMd5, String fileExt) {
String one = fileMd5.substring(0, 1);
String two = fileMd5.substring(1, 2);
return one + "/" + two + "/" + fileMd5 + "/" +
Base64.encode(String.valueOf(System.currentTimeMillis()).getBytes()) + "." + fileExt;
}
/**
* @param request
* @param response
* @param absPath
* @throws IOException
*/
//文件下载
public static void fileDownload(HttpServletRequest request, HttpServletResponse response, String absPath) throws IOException {
//读取路径下面的文件
File file = new File(absPath);
if (file == null) {
ExceptionCast.cast(-1, "文件不存在!");
}
String fileName = file.getName();
//获取文件后缀名格式
String ext = fileName.substring(fileName.indexOf("."));
//判断图片格式,设置相应的输出文件格式
if (ext.equals(".jpg")) {
response.setContentType("image/jpeg");
} else if (ext.equals(".JPG")) {
response.setContentType("image/jpeg");
} else if (ext.equals(".png")) {
response.setContentType("image/png");
} else if (ext.equals(".PNG")) {
response.setContentType("image/png");
} else {
response.setContentType("application/octet-stream");
}
//将响应的类型设置为图片
response.setHeader("Content-Disposition",
"attachment;filename=" + RYFileUtils.setFileDownloadHeader(request, fileName));
FileInputStream inputStream = new FileInputStream(file);
//输出流
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int length;
while ((length = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, length);
}
inputStream.close();
outputStream.close();
outputStream.flush();
}
public static void fileDownload(HttpServletRequest request, HttpServletResponse response, InputStream inputStream) throws IOException {
//输出流
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int length;
while ((length = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, length);
}
outputStream.flush();
inputStream.close();
outputStream.close();
}
public static boolean deleteFile(String pathname) {
File file = new File(pathname);
if (file.exists()) {
boolean flag = file.delete();
if (flag) {
File[] files = file.getParentFile().listFiles();
if (files == null || files.length == 0) {
file.getParentFile().delete();
}
}
return flag;
}
return false;
}
public static String fileMd5(InputStream inputStream) {
try {
return DigestUtils.md5Hex(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String getPath() {
return "/" + LocalDate.now().toString().replace("-", "/") + "/";
}
/**
* 将文本写入文件
*
* @param value
* @param path
*/
public static void saveTextFile(String value, String path) {
FileWriter writer = null;
try {
File file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
writer = new FileWriter(file);
writer.write(value);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java FileUtils 文件操作
最新推荐文章于 2022-09-13 16:00:48 发布