package com.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Scanner;
@Slf4j
public class FileUtils {
/* 文件路径 转 MultipartFile*/
public static MultipartFile getMultipartFile(String filePath) {
File file = new File(filePath);
FileItem item = new DiskFileItemFactory().createItem(file.getName(), MediaType.MULTIPART_FORM_DATA_VALUE, true, file.getName());
try {
InputStream input = new FileInputStream(file);
OutputStream os = item.getOutputStream();
IOUtils.copy(input, os);// 流转移
} catch (IOException ioe) {
log.error("getMultipartFile IOException", ioe);
}
return new CommonsMultipartFile(item);
}
/**
* 读取文件内容
*/
public static String readFileContent(MultipartFile file) {
StringBuilder value = new StringBuilder("");
// 读取文件内容到Stream流中,按行读取
try (Scanner sc = new Scanner(file.getInputStream())) {
while (sc.hasNextLine()) { //按行读取字符串
value.append(sc.nextLine().trim());
value.append("\n");
}
} catch (IOException e) {
log.error("readFileContent IOException:", e);
}
return value.toString();
}
/**
* 通用文件上传
*
* @param file 需要上传的文件
* @param uploadPath 上传到的路径
*/
public static boolean uploadFile(MultipartFile file, String uploadPath) {
File newFile = new File(uploadPath);
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
try {
InputStream inputStream = file.getInputStream();
OutputStream os = null;
byte[] bs = new byte[1024];
int len;
os = new FileOutputStream(newFile);
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
inputStream.close();
} catch (IOException ioe) {
log.error("uploadFile IOException:", ioe);
}
return true;
}
/**
* 通用文件下载
*
* @param filePath 需要下载的文件
*/
public static void downloadFile(HttpServletResponse response, String filePath) {
//根据文件路径获取文件
File packetFile = new File(filePath);
String fileName = packetFile.getName();
File file = new File(filePath);
if (file.exists()) {
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
log.error("downloadFile UnsupportedEncodingException", e);
}
// 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (IOException ioe) {
log.error("downloadFile IOException:", ioe);
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("关闭bis流异常:", e);
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
log.error("关闭fis流异常:", e);
}
}
} else {
log.info(filePath + ":文件下载失败,文件不存在!");
}
}
}
文件工具类(路径转MultipartFile,读取文件内容,通用文件上传,通用文件下载)
最新推荐文章于 2024-07-03 15:24:31 发布
2498

被折叠的 条评论
为什么被折叠?



