119 行
3.6 KiB
Java
119 行
3.6 KiB
Java
package com.weilab.biology.util;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.*;
|
|
|
|
/**
|
|
* 文件读取工具类
|
|
*/
|
|
public class FileUtils {
|
|
|
|
/**
|
|
* MultipartFile 转换成File
|
|
*
|
|
* @param multfile 原文件类型
|
|
* @return File
|
|
*/
|
|
public static File multipartToFile(MultipartFile multfile) {
|
|
File file = null;
|
|
try {
|
|
file = File.createTempFile("prefix", "_" + multfile.getOriginalFilename());
|
|
multfile.transferTo(file);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return file;
|
|
}
|
|
|
|
/**
|
|
* 读取文件内容,作为字符串返回
|
|
*/
|
|
public static String readFileAsString(String filePath) throws IOException {
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
throw new FileNotFoundException(filePath);
|
|
}
|
|
|
|
return readFileAsString(file);
|
|
}
|
|
|
|
/**
|
|
* 读取文件内容,作为字符串返回
|
|
*/
|
|
public static String readFileAsString(File file) throws IOException {
|
|
StringBuilder sb = new StringBuilder((int) (file.length()));
|
|
// 创建字节输入流
|
|
FileInputStream fis = new FileInputStream(file);
|
|
// 创建一个长度为10240的Buffer
|
|
byte[] bbuf = new byte[10240];
|
|
// 用于保存实际读取的字节数
|
|
int hasRead = 0;
|
|
while ((hasRead = fis.read(bbuf)) > 0) {
|
|
sb.append(new String(bbuf, 0, hasRead));
|
|
}
|
|
fis.close();
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* 根据文件路径读取byte[] 数组
|
|
*/
|
|
public static byte[] readFileAsBytes(String filePath) throws IOException {
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
throw new FileNotFoundException(filePath);
|
|
} else {
|
|
return readFileAsBytes(file);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据文件读取byte[]数组
|
|
*/
|
|
public static byte[] readFileAsBytes(File file) throws IOException {
|
|
if (file == null) {
|
|
throw new FileNotFoundException();
|
|
} else {
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
|
|
BufferedInputStream in = null;
|
|
|
|
try {
|
|
in = new BufferedInputStream(new FileInputStream(file));
|
|
short bufSize = 1024;
|
|
byte[] buffer = new byte[bufSize];
|
|
int len1;
|
|
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
|
|
bos.write(buffer, 0, len1);
|
|
}
|
|
|
|
return bos.toByteArray();
|
|
} finally {
|
|
try {
|
|
if (in != null) {
|
|
in.close();
|
|
}
|
|
} catch (IOException var14) {
|
|
var14.printStackTrace();
|
|
}
|
|
bos.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static void writeStringToFile(String filePath, String content) throws IOException {
|
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
|
|
bufferedOutputStream.write(content.getBytes());
|
|
bufferedOutputStream.flush();
|
|
bufferedOutputStream.close();
|
|
}
|
|
|
|
public static void writeByteToFile(String filePath, byte[] content) throws IOException {
|
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
|
|
bufferedOutputStream.write(content);
|
|
bufferedOutputStream.flush();
|
|
bufferedOutputStream.close();
|
|
}
|
|
}
|