public Map<String, Object> checkWithIn(Map<String, Object> params, boolean flag) {
InputStream in = (InputStream) MapUtils.getObject(params, "in");
Map<String, Object> resMap = new HashMap<String, Object>();
BufferedInputStream bis = null;
ByteArrayOutputStream fos = null;
ByteArrayInputStream bai = null;
byte[] buffer = new byte[NumberConstant.CHANGESIZE];
int len = 0;
int maxsize = NumberConstant.TWO * NumberConstant.CHANGESIZE * NumberConstant.CHANGESIZE;
String picDesc = MapUtils.getString(params, SpsConstant.PIC_DESC, "");
String prevName = MapUtils.getString(params, "prevName", "");
try {
bis = new BufferedInputStream(in);
fos = new ByteArrayOutputStream(SpsConstant.SIZECHANGE);
// 校验图片大小
while (-1 != (len = bis.read(buffer))) {
fos.write(buffer, 0, len);
if (fos.size() >= maxsize) {
resMap.put("error_mes", "size_over");
resMap.put(SpsConstant.PIC_DESC, prevName);
return resMap;
}
}
byte[] content = fos.toByteArray();
//业务代码...
} catch (IOException e) {
resMap.put(ERROR_MES, EXCEPTION);
resMap.put(SpsConstant.PIC_DESC, picDesc);
} finally {
close(bis, fos, bai, resMap);
fos = null;
bis = null;
bai = null;
}
return resMap;
}
private void close(BufferedInputStream bis, ByteArrayOutputStream fos, ByteArrayInputStream bai,
Map<String, Object> resMap) {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
resMap.put(ERROR_MES, EXCEPTION);
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
resMap.put(ERROR_MES, EXCEPTION);
}
}
if (null != bai) {
try {
bai.close();
} catch (IOException e) {
resMap.put(ERROR_MES, EXCEPTION);
}
}
}
文件上传的代码啊,没有多少东西啊。。为什么放到生产环境上就会OOM啊。大神给指点下呢,我知道这个方案不是很好,应该在框架上或者前段来判断文件大小,但是现在不能有太多的代码改动,先分析出来问题哈。。。。
压测没有问题,我本地各种线程并发都试过,没出现问题啊。。为什么生产上就会有这个问题呢?
报OOM的就是
byte[] content = fos.toByteArray();
,fos太大??不合理啊?
理论上如果很大的话上面循环的时候有判断啊,现在没有走上面的,但是下面OOM了,finally也释放了啊。请大神指点~~~