好的,我来详细讲一下如何限制最大读取大小。
为什么要限制最大读取大小?
- 防止 OOM(OutOfMemoryError):如果文件太大,全部读到内存可能导致 JVM 崩溃。
- 防止恶意文件攻击:比如有人上传一个超大文件,故意耗尽服务器资源。
- 提高系统稳定性:限制文件大小,可以避免处理超大文件导致的性能问题。
怎么做?
-
在读取 InputStream 时,边读边判断大小
- 用一个计数器记录已经读取的字节数。
- 如果超过阈值(比如 100MB),直接抛出异常,拒绝处理。
-
使用 ByteArrayOutputStream 或 CountingInputStream
ByteArrayOutputStream 可以在写入时检查大小。
CountingInputStream 是 Apache Commons IO 提供的一个工具类,可以直接统计读取的字节数。
示例代码:
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CountingInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamUtils {
private static final int MAX_ALLOWED_SIZE = 100 * 1024 * 1024; // 100MB
// 方法1:使用 ByteArrayOutputStream
public static byte[] readInputStreamWithLimit(InputStream inputStream) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[8192];
int len;
long totalSize = 0;
while ((len = inputStream.read(buffer)) != -1) {
totalSize += len;
if (totalSize > MAX_ALLOWED_SIZE) {
throw new IOException("File size exceeds the maximum allowed limit: " + MAX_ALLOWED_SIZE + " bytes");
}
baos.write(buffer, 0, len);
}
return baos.toByteArray();
}
}
// 方法2:使用 CountingInputStream
public static void readInputStreamWithLimit2(InputStream inputStream) throws IOException {
try (CountingInputStream cis = new CountingInputStream(inputStream)) {
byte[] buffer = new byte[8192];
int len;
while ((len = cis.read(buffer)) != -1) {
if (cis.getByteCount() > MAX_ALLOWED_SIZE) {
throw new IOException("File size exceeds the maximum allowed limit: " + MAX_ALLOWED_SIZE + " bytes");
}
// 处理读取的数据(比如写入另一个流)
}
}
}
// 方法3:直接包装 InputStream,超过大小直接抛出异常
public static InputStream limitInputStream(InputStream inputStream) {
return new LimitedInputStream(inputStream, MAX_ALLOWED_SIZE);
}
private static class LimitedInputStream extends InputStream {
private final InputStream delegate;
private final long maxSize;
private long count = 0;
public LimitedInputStream(InputStream delegate, long maxSize) {
this.delegate = delegate;
this.maxSize = maxSize;
}
@Override
public int read() throws IOException {
int b = delegate.read();
if (b != -1) {
count++;
checkSize();
}
return b;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = delegate.read(b, off, len);
if (read > 0) {
count += read;
checkSize();
}
return read;
}
private void checkSize() throws IOException {
if (count > maxSize) {
throw new IOException("File size exceeds the maximum allowed limit: " + maxSize + " bytes");
}
}
@Override
public void close() throws IOException {
delegate.close();
}
}
}
使用示例:
// 方法1
try (InputStream inputStream = new FileInputStream("test.xlsx")) {
byte[] data = InputStreamUtils.readInputStreamWithLimit(inputStream);
// 处理 data
} catch (IOException e) {
if (e.getMessage().contains("exceeds the maximum allowed limit")) {
System.out.println("文件太大,不处理!");
} else {
e.printStackTrace();
}
}
// 方法2
try (InputStream inputStream = new FileInputStream("test.xlsx")) {
InputStreamUtils.readInputStreamWithLimit2(inputStream);
} catch (IOException e) {
// 处理异常
}
// 方法3
try (InputStream inputStream = InputStreamUtils.limitInputStream(new FileInputStream("test.xlsx"))) {
// 使用这个 inputStream,后续读取会自动检查大小
} catch (IOException e) {
// 处理异常
}
总结:
- 可以在读取 InputStream 时,实时检查已读取的字节数。
- 超过阈值(比如 100MB)直接抛出异常,拒绝处理。
- 可以用
ByteArrayOutputStream 或 CountingInputStream 来实现。
- 也可以自定义一个
LimitedInputStream,包装原始流,自动检查大小。
这样可以有效防止超大文件导致的 OOM 或恶意攻击。
—
如果需要,我还可以给你写一个更完整的工具类,比如:
- 支持多种文件类型(.xlsx、.xls、.csv 等)
- 支持多种限制策略(大小、行数、列数等)
- 支持自定义异常处理
直接告诉我你的需求!🚀