调整上传照片流关闭的问题

This commit is contained in:
limqhz
2020-01-31 22:37:57 +08:00
parent 32c3396a80
commit 6fb149ae18

View File

@@ -31,9 +31,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.regex.Matcher;
@@ -499,16 +497,14 @@ public class MemberService extends BaseServiceImpl {
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
byte[] byt = new byte[inputStream.available()];
inputStream.read(byt);
Member member = memberMapper.findById(memberId);
if (member != null) {
String imgUrl = ossClientUtil.uploadImg(new ByteArrayInputStream(byt), file.getOriginalFilename());
String imgUrl = ossClientUtil.uploadImg(new BufferedInputStream(inputStream), file.getOriginalFilename());
logger.info(memberId + "上传oss文件成功." + imgUrl);
Integer subjectId = null;
if (faceService.login()){
Integer faceId = faceService.uploadFace(new ByteArrayInputStream(byt));
byt = null;
ByteArrayInputStream byteArrayInputStream = getByteArrayInputStream(file);
Integer faceId = faceService.uploadFace(byteArrayInputStream);
subjectId = faceService.addSubject(faceId, member);
logger.info(faceId + "&&" + subjectId);
}
@@ -534,6 +530,34 @@ public class MemberService extends BaseServiceImpl {
}
}
// 将文件中流转换为字节数组,通过缓存数据的方式转换防止内存溢出
private ByteArrayInputStream getByteArrayInputStream(MultipartFile file) {
ByteArrayInputStream result = null;
InputStream in = null;
ByteArrayOutputStream bao = null;
try{
in = file.getInputStream();
byte[] buff = new byte[2048];
int temp = 0;
bao = new ByteArrayOutputStream();
while((temp = in.read(buff)) != -1) {
bao.write(buff, 0, temp);
}
byte[] data = bao.toByteArray();
result = new ByteArrayInputStream(data);
}catch (Exception e){
return null;
}finally {
try {
in.close();
bao.close();
} catch (IOException e) {
// do nothing
}
}
return result;
}
/**
* 通过人脸识别的faceID获取用户
*