我正在使用上面的代码上传文件,代码工作正常。
我的问题是,是否可以仅在图像上传期间将图像限制为 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/