gpt4 book ai didi

java - 是否可以限制图像大小并仅上传特定大小

转载 作者:行者123 更新时间:2023-11-30 08:00:14 26 4
gpt4 key购买 nike

我正在使用上面的代码上传文件,代码工作正常。

我的问题是,是否可以仅在图像上传期间将图像限制为 75 kb?

如果超过 75 kb,我不想抛出异常,而是继续上传我得到的内容

private void writeToFile(InputStream uploadedInputStream,String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

最佳答案

public final static int MAX_SIZE = 75000;

private void writeToFile(InputStream uploadedInputStream,String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
int size = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while (size < MAX_SIZE && (read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
size += read;
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

关于java - 是否可以限制图像大小并仅上传特定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com