调整上传照片流关闭的问题
This commit is contained in:
@@ -32,6 +32,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -494,16 +496,22 @@ public class MemberService extends BaseServiceImpl {
|
||||
}
|
||||
}
|
||||
synchronized (("face" + memberId).intern()) {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
byte[] byt = new byte[file.getInputStream().available()];
|
||||
file.getInputStream().read(byt);
|
||||
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());
|
||||
faceService.login();
|
||||
Integer faceId = faceService.uploadFace(new ByteArrayInputStream(byt));
|
||||
Integer subjectId = faceService.addSubject(faceId, member);
|
||||
logger.info(faceId + "&&" + subjectId);
|
||||
logger.info(memberId + "上传oss文件成功." + imgUrl);
|
||||
Integer subjectId = null;
|
||||
if (faceService.login()){
|
||||
Integer faceId = faceService.uploadFace(new ByteArrayInputStream(byt));
|
||||
byt = null;
|
||||
subjectId = faceService.addSubject(faceId, member);
|
||||
logger.info(faceId + "&&" + subjectId);
|
||||
}
|
||||
if (memberFaceInfo != null) {
|
||||
//删除原有的subject
|
||||
faceService.deleteSubject(memberFaceInfo.getFaceId());
|
||||
@@ -516,6 +524,12 @@ public class MemberService extends BaseServiceImpl {
|
||||
} catch (Exception e) {
|
||||
logger.info("unknown error",e);
|
||||
throw new ServiceException("设置人脸失败,请稍后再试");
|
||||
}finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class FaceService {
|
||||
/**
|
||||
* face++登录
|
||||
*/
|
||||
public void login() throws IOException {
|
||||
public boolean login() throws IOException {
|
||||
JsonObject param = new JsonObject();
|
||||
param.addProperty("username", account);
|
||||
param.addProperty("password", pwd);
|
||||
@@ -63,7 +63,7 @@ public class FaceService {
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
LoginResponse loginResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), LoginResponse.class);
|
||||
logger.info("登录成功" + ToStringBuilder.reflectionToString(loginResponse));
|
||||
|
||||
return true;
|
||||
} else {
|
||||
logger.error("face++登陆失败!");
|
||||
throw new FaceServiceException("登录失败");
|
||||
@@ -150,6 +150,7 @@ public class FaceService {
|
||||
HttpEntity httpEntity = multipartEntityBuilder.build();
|
||||
httpPost.setEntity(httpEntity);
|
||||
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||
face.close();
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
AddFaceResponse addFaceResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), AddFaceResponse.class);
|
||||
if (addFaceResponse.getCode() == 0) {
|
||||
@@ -168,6 +169,11 @@ public class FaceService {
|
||||
public void deleteSubject(Integer subjectId) throws IOException {
|
||||
HttpDelete httpDelete = new HttpDelete("http://" + faceUrl + "/subject/" + subjectId);
|
||||
CloseableHttpResponse response = httpClient.execute(httpDelete);
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
logger.info("删除头像成功 subjectId" + subjectId);
|
||||
} else {
|
||||
logger.info("删除头像失败 subjectId" + subjectId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user