/**
* 对上传的文件流进行处理
*
* @param file
* @param userInfo
* @return
*/
public boolean uploadVoice(MultipartFile file, UserInfo userInfo) {
List<HashMap<String, Object>> taskList = null;
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
try {
if("zip".equals(type.toLowerCase())){
taskList = zip("GBK", file, userInfo);
}else if("rar".equals(type.toLowerCase())){
taskList = rar(file, userInfo);
}
} catch (Exception e) {
try {
LOGGER.error("[VoiceMarkServiceImpl][uploadVoice]:-------GBK编码解压失败,使用UTF-8开始解压", e);
if("zip".equals(type.toLowerCase())){
taskList = zip("UTF-8", file, userInfo);
}else if("rar".equals(type.toLowerCase())){
taskList = rar(file, userInfo);
}
} catch (Exception e2) {
LOGGER.error("[VoiceMarkServiceImpl][uploadVoice]:-------UTF-8解压错误", e);
return false;
}
}
if(taskList == null){
return false;
}else{
dao.batchAdd(taskList);
return true;
}
}
/**
* 处理上传的压缩包,解压获取录音文件属性
*
* @param charset
* 字符编码
* @param zipPath
* @param uploadPath
* 上传地址
* @return 每个录音文件的属性,用来插入数据库
* @throws Exception
*/
public List<HashMap<String, Object>> zip(String charset,
MultipartFile mfile, UserInfo userInfo) throws Exception {
LOGGER.info("[ServiceImpl][zip]:-------获取的上传路径uploadPath="
+ Constants
.getAppConfigProperty("SAVEPATH"));
List<HashMap<String, Object>> fileList = new ArrayList<HashMap<String, Object>>();
// 解决zip文件中有中文目录或者中文文件
// ZipFile zip=new ZipFile(zipFile,Charset.forName(charset));//输入源zip路径
try (ZipInputStream zip = new ZipInputStream(mfile.getInputStream(),
Charset.forName(charset));) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sdf.format(new Date());
String romdom = romdom();
LOGGER.info("[ServiceImpl][zip]:-------任务id="
+ romdom);
ZipEntry entry = null;
String uuid = null;
while ((entry = zip.getNextEntry()) != null) {
uuid = UUID.randomUUID().toString().replace("-", "");// 流水号
String name = entry.getName();
String zipEntryName = name.replace(
name.substring(0, name.lastIndexOf(".")), uuid);
String outPath = (Constants
.getAppConfigProperty("SAVEPATH") + File.separator + romdom
+ File.separator + zipEntryName);
String savePath = (File.separator + romdom + File.separator + zipEntryName);// 保存的路径,用相对路径
HashMap<String, Object> fileMap = new HashMap<String, Object>();
fileMap.put("KEY", userInfo.getName());// key
fileMap.put("ID", romdom);// 任务id
fileMap.put("CODE", userInfo.getCode());// 账号
fileMap.put("INSERT_TIME", now);
fileMap.put("ID", uuid);// 主键
fileMap.put("FILE_PATH", savePath);// 文件路径
fileList.add(fileMap);
// 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0,
outPath.lastIndexOf(File.separator)));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
// 输出文件路径信息
LOGGER.info("[ServiceImpl][uploadVoice]:-------输出文件路径信息="
+ outPath);
try (OutputStream out = new FileOutputStream(outPath);){
byte[] buf = new byte[1024];
int len = 0;
while ((len = zip.read(buf)) != -1) {
out.write(buf, 0, len);
}
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上传zip错误=" , e);
return null;
}
}
LOGGER.info("[ServiceImpl][uploadVoice]:-------上传完毕,文件的信息集合="
+ fileList);
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上传zip错误=" , e);
fileList = null;
}
return fileList;
}
public List<HashMap<String, Object>> rar(MultipartFile file, UserInfo userInfo) {
LOGGER.info("[ServiceImpl][rar]:-------获取的上传路径uploadPath="
+ Constants
.getAppConfigProperty("SAVEPATH"));
List<HashMap<String, Object>> fileList = new ArrayList<HashMap<String, Object>>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sdf.format(new Date());
String romdom = romdom();
LOGGER.info("[ServiceImpl][uploadVoice]:-------任务id="
+ romdom);
String rarSavePath = Constants.getAppConfigProperty("SAVEPATH") + File.separator + romdom;
File pathFile = new File(rarSavePath + File.separator + file.getOriginalFilename());
if(!pathFile.exists()) {
pathFile.mkdirs();
}
try {
file.transferTo(pathFile);//暂时保存rar
try (Archive archive = new Archive(pathFile)){
FileHeader fh = archive.nextFileHeader();
File destFileName = null;
String uuid = null;
while (fh != null) {
uuid = UUID.randomUUID().toString().replace("-", "");// 流水号
String name = fh.getFileNameString();
String zipEntryName = name.replace(
name.substring(0, name.lastIndexOf(".")), uuid);
String outPath = rarSavePath + File.separator + zipEntryName;
String savePath = (File.separator + romdom + File.separator + zipEntryName);// 保存的路径,用相对路径
HashMap<String, Object> fileMap = new HashMap<String, Object>();
fileMap.put("KEY", userInfo.getEntName());// key
fileMap.put("TASK_ID", romdom);// 任务id
fileMap.put("CODE", userInfo.getUserCode());//账号
fileMap.put("INSERT_TIME", now);
fileMap.put("ID", uuid);// 主键
fileMap.put("FILE_PATH", savePath);// 文件路径
fileList.add(fileMap);
destFileName = new File(outPath);
if (fh.isDirectory()) {
if (!destFileName.exists()) {
destFileName.mkdirs();
}
fh = archive.nextFileHeader();
continue;
}
if (!destFileName.getParentFile().exists()) {
destFileName.getParentFile().mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(destFileName)){
archive.extractFile(fh, fos);
}
fh = archive.nextFileHeader();
}
}
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上传rar错误=" , e);
fileList = null;
} finally{
pathFile.delete();
}
return fileList;
}