调整上传照片流关闭的问题
This commit is contained in:
@@ -31,9 +31,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@@ -499,16 +497,14 @@ public class MemberService extends BaseServiceImpl {
|
|||||||
InputStream inputStream = null;
|
InputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
inputStream = file.getInputStream();
|
inputStream = file.getInputStream();
|
||||||
byte[] byt = new byte[inputStream.available()];
|
|
||||||
inputStream.read(byt);
|
|
||||||
Member member = memberMapper.findById(memberId);
|
Member member = memberMapper.findById(memberId);
|
||||||
if (member != null) {
|
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);
|
logger.info(memberId + "上传oss文件成功." + imgUrl);
|
||||||
Integer subjectId = null;
|
Integer subjectId = null;
|
||||||
if (faceService.login()){
|
if (faceService.login()){
|
||||||
Integer faceId = faceService.uploadFace(new ByteArrayInputStream(byt));
|
ByteArrayInputStream byteArrayInputStream = getByteArrayInputStream(file);
|
||||||
byt = null;
|
Integer faceId = faceService.uploadFace(byteArrayInputStream);
|
||||||
subjectId = faceService.addSubject(faceId, member);
|
subjectId = faceService.addSubject(faceId, member);
|
||||||
logger.info(faceId + "&&" + subjectId);
|
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获取用户
|
* 通过人脸识别的faceID获取用户
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user