@Override
public ComponentCheckDTO importZip(MultipartFile multipartFile) throws IOException {
ComponentCheckDTO componentCheckDTO = new ComponentCheckDTO();
//获取ZIP输入流(指定字符集)
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(multipartFile.getInputStream()), StandardCharsets.UTF_8);
//定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
ZipEntry ze = null;
//循环遍历
while ((ze = zipInputStream.getNextEntry()) != null) {
System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
String path = System.getProperty("java.io.tmpdir");
String fileName = ze.getName();
String fileFullName = path + "\\" + fileName;
if (ze.isDirectory()) {
createDirIfNotExist(fileFullName);
} else {
System.out.println("文件内容:");
File file = new File(fileFullName);
createFileIfNotExist(file);
FileOutputStream fos = new FileOutputStream(fileFullName);
int tmp;
byte[] b = new byte[1024];
StringBuffer stringBuffer = new StringBuffer();
while ((tmp = zipInputStream.read(b)) != -1) {
//写入到目标文件中
fos.write(b, 0, tmp);
String data = new String(b, 0, tmp);
stringBuffer.append(data);
System.out.println(data);
}
fos.close();
// 上传fastDFS
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile uploadFile = new MockMultipartFile("file", file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
JSONObject uploadFileObject = fileFeignService.uploadFile(uploadFile);
if ("0".equals(uploadFileObject.get("code"))) {
JSONObject data = JSONUtil.parseObj(uploadFileObject.get("data"));
String fastDFSId = data.getStr("id");
String filePath = data.getStr("filePath");
String[] split = filePath.split("/");
String fastDFSName = split[split.length - 1];
}
if (StringUtils.isNotBlank(stringBuffer) && stringBuffer.toString().startsWith("{")) {
// todo 存入映射管理表
JSONObject jsonObject = JSONUtil.parseObj(stringBuffer);
System.out.println("jsonObject: " + jsonObject);
}
// todo 删除文件
boolean delete = file.delete();
System.out.println(delete ? "删除成功!" : "删除失败...");
}
zipInputStream.closeEntry();
}
//关闭流
zipInputStream.closeEntry();
return componentCheckDTO;
}
post曼上传压缩包,后端解析并上传fastDFS
最新推荐文章于 2024-08-16 23:08:28 发布