private String fileRootPath = "D:\\wwwroot\\java\\pic\\temp\\";
private static final String CONTENT_DISPOSITION = "content-disposition";
private static final String FILENAME_KEY = "filename=";
private static final String FILENAME_WITH_CHARSET_KEY = "filename*=";
/**
* 摄像头多图上传
*
* @return
*/
@RequestMapping(value = "/viid/images/report/all/")
public ApiResponse uploads(HttpServletRequest request) {
List<FileItem> list = getFileItem(request);
String faceBase64 = "";
//遍历list,得到用于封装第一个上传输入项数据fileItem对象
for (FileItem item : list) {
if (item.isFormField()) {
//得到的是普通输入项
String name = item.getFieldName(); //得到输入项的名称
String value = item.getString();
System.out.println(name + "=" + value);
} else {
//得到上传输入项
String filename = item.getName(); //得到上传文件名 C:\Documents and Settings\ThinkPad\桌面\1.txt
System.out.println(filename);
}
MultipartFile mfile = new CommonsMultipartFile(item);
//multipartUpload(mfile);
// 通过base64来转化图片
String filePath = multipartUpload(mfile);
faceBase64 = localImageToBase64(filePath);
try {
semaphore.acquire();
FaceFeature thisFaceFeature = faceExtract.execute(faceBase64);
UserFace userFace = faceCompare.execute(thisFaceFeature);
if (userFace == null) throw new ApiException(1103);
return ApiResponse.successData(userFace);
} catch (ApiException e) {
return ApiResponse.error(e.getCode(), e.getMessage());
} catch (Exception e) {
return ApiResponse.error(0, e.getMessage());
} finally {
semaphore.release();
}
}
return ApiResponse.error(0, "识别失败");
}
/**
* 获取到上传文件
*
* @param request
* @return
*/
private List<FileItem> getFileItem(HttpServletRequest request) {
try {
//1.得到解析器工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.得到解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//3.判断上传表单的类型
if (!upload.isMultipartContent(request)) {
//上传表单为普通表单,则按照传统方式获取数据即可
return null;
}
//为上传表单,则调用解析器解析上传数据
List<FileItem> fileList = upload.parseRequest(request); //FileItem
return fileList;
} catch (Exception e) {
return null;
}
}
/**
* 保存上传文件
*
* @param file
* @return
*/
private String multipartUpload(MultipartFile file) {
String filePath = "";
// 上传简单文件名
String originalFilename = file.getOriginalFilename();
// 存储路径
filePath = new StringBuilder(fileRootPath)
.append(System.currentTimeMillis())
.append(originalFilename)
.toString();
try {
// 保存文件
System.out.println("保存文件");
System.out.println(filePath);
file.transferTo(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
System.out.printf("上传异常");
}
return filePath;
}
网友评论