|
@@ -53,14 +53,13 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
public ShiwanFileUpload saveOrUpdateFile(MultipartFile file, String userId, Long accountId, String fileType, Long parentId, String uuid) {
|
|
public ShiwanFileUpload saveOrUpdateFile(MultipartFile file, String userId, Long accountId, String fileType, Long parentId, String uuid) {
|
|
ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId);
|
|
ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId);
|
|
StringBuffer path = new StringBuffer();
|
|
StringBuffer path = new StringBuffer();
|
|
- String uuids = UUID.randomUUID().toString();
|
|
|
|
- path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuids).append("/");
|
|
|
|
- log.info("----------服务器下载地址:{}", path.toString());
|
|
|
|
|
|
+ path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(UUID.randomUUID().toString()).append("/");
|
|
|
|
+ log.info("----------下载到服务器地址:{}", path.toString());
|
|
ShiwanFileUpload shiwanFileUpload = null;
|
|
ShiwanFileUpload shiwanFileUpload = null;
|
|
- String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
|
|
|
+ String newFileUrl = FileUtil.approvalFile(file, path.toString());
|
|
String md5 = null;
|
|
String md5 = null;
|
|
try {
|
|
try {
|
|
- md5 = LoadFileUtil.getMD5(newUrl);
|
|
|
|
|
|
+ md5 = LoadFileUtil.getMD5(newFileUrl);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
log.error("获取MD5签名失败", e);
|
|
log.error("获取MD5签名失败", e);
|
|
}
|
|
}
|
|
@@ -68,7 +67,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
existFiles.setSignature(md5);
|
|
existFiles.setSignature(md5);
|
|
shiwanFileUpload = updateFile(file, existFiles);
|
|
shiwanFileUpload = updateFile(file, existFiles);
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
- List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -80,7 +79,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
fileUpload.setParentId(parentId);
|
|
fileUpload.setParentId(parentId);
|
|
shiwanFileUpload = saveFile(file, fileUpload, uuid);
|
|
shiwanFileUpload = saveFile(file, fileUpload, uuid);
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
if (!Check.isNull(shiwanFileUpload)) {
|
|
- List<String> urlList = unzipFiles(shiwanFileUpload, path.toString());
|
|
|
|
|
|
+ List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
saveInnerFiles(shiwanFileUpload, urlList);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -169,23 +168,27 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuid).append("/");
|
|
path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(uuid).append("/");
|
|
log.info("----------服务器下载地址:{}", path.toString());
|
|
log.info("----------服务器下载地址:{}", path.toString());
|
|
//解压zip文件
|
|
//解压zip文件
|
|
- List<String> urlList = unzipFiles(fileUpload, path.toString());
|
|
|
|
|
|
+ List<String> urlList = unzipFiles(fileUpload, path.toString(), null);
|
|
|
|
+ String newFilePath = "";
|
|
if (null != urlList && urlList.size() > 0) {
|
|
if (null != urlList && urlList.size() > 0) {
|
|
Iterator it = urlList.iterator();
|
|
Iterator it = urlList.iterator();
|
|
while (it.hasNext()) {
|
|
while (it.hasNext()) {
|
|
String url = it.next().toString();
|
|
String url = it.next().toString();
|
|
String fileName = url.substring(url.lastIndexOf("/") + 1);
|
|
String fileName = url.substring(url.lastIndexOf("/") + 1);
|
|
if (updateName.equals(fileName)) {
|
|
if (updateName.equals(fileName)) {
|
|
|
|
+ newFilePath = url.substring(0, url.lastIndexOf("/") + 1);
|
|
//移除被替换的对象
|
|
//移除被替换的对象
|
|
it.remove();
|
|
it.remove();
|
|
|
|
+ deletelocalFiles(url);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//添加上新的对象
|
|
//添加上新的对象
|
|
- String newUrl = FileUtil.approvalFile(file, path.toString());
|
|
|
|
|
|
+ String newUrl = FileUtil.approvalFile(file, newFilePath);
|
|
urlList.add(newUrl);
|
|
urlList.add(newUrl);
|
|
//压缩zip文件
|
|
//压缩zip文件
|
|
- String newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPYFROM_").concat(fileUpload.getFileName());
|
|
|
|
|
|
+ String newFileName = DateUtils.getNowDate(DateUtils.LONG_FORMAT).concat("_COPY-FROM_").concat(fileUpload.getFileName());
|
|
return createNewZipFiles(urlList, path.toString(), newFileName, updateName, userId, accountId, fileType);
|
|
return createNewZipFiles(urlList, path.toString(), newFileName, updateName, userId, accountId, fileType);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -202,25 +205,27 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
* @throws
|
|
* @throws
|
|
* @author ZHAOXA
|
|
* @author ZHAOXA
|
|
*/
|
|
*/
|
|
- private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path) {
|
|
|
|
|
|
+ private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path, String innerZipPath) {
|
|
List<String> urlList = new ArrayList<>();
|
|
List<String> urlList = new ArrayList<>();
|
|
|
|
+ //下载到本地服务器
|
|
|
|
+ if (Check.isNull(innerZipPath)) {
|
|
|
|
+ innerZipPath = FileUtil.writeFiles(path.concat("oldZip/"), fileUpload.getFileUrl(), fileUpload.getCfileName());
|
|
|
|
+ }
|
|
try {
|
|
try {
|
|
- //下载到本地服务器
|
|
|
|
- String innerZipPath = FileUtil.writeFiles(path.concat("oldZip/"), fileUpload.getFileUrl(), fileUpload.getCfileName());
|
|
|
|
- log.info("----------zip包解压地址:{}", innerZipPath);
|
|
|
|
|
|
+ log.info("----------zip包下载地址:{}", innerZipPath);
|
|
ZipFile zipFile = new ZipFile(innerZipPath, Charset.forName("GBK"));
|
|
ZipFile zipFile = new ZipFile(innerZipPath, Charset.forName("GBK"));
|
|
Enumeration<?> enumeration = zipFile.entries();
|
|
Enumeration<?> enumeration = zipFile.entries();
|
|
ZipEntry zipEntry = null;
|
|
ZipEntry zipEntry = null;
|
|
- path.concat("innerFile/");
|
|
|
|
|
|
+ String innerPath = path.concat("innerFiles/");
|
|
while (enumeration.hasMoreElements()) {
|
|
while (enumeration.hasMoreElements()) {
|
|
zipEntry = (ZipEntry) enumeration.nextElement();
|
|
zipEntry = (ZipEntry) enumeration.nextElement();
|
|
// 如果是文件夹,就创建个文件夹
|
|
// 如果是文件夹,就创建个文件夹
|
|
if (zipEntry.isDirectory()) {
|
|
if (zipEntry.isDirectory()) {
|
|
- String dirPath = path + zipEntry.getName();
|
|
|
|
|
|
+ String dirPath = innerPath + zipEntry.getName();
|
|
File dir = new File(dirPath);
|
|
File dir = new File(dirPath);
|
|
dir.mkdirs();
|
|
dir.mkdirs();
|
|
} else {
|
|
} else {
|
|
- String url = path + zipEntry.getName();
|
|
|
|
|
|
+ String url = innerPath + zipEntry.getName();
|
|
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
|
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
|
|
File targetFile = new File(url);
|
|
File targetFile = new File(url);
|
|
// 保证这个文件的父文件夹必须要存在
|
|
// 保证这个文件的父文件夹必须要存在
|
|
@@ -232,20 +237,20 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
InputStream is = zipFile.getInputStream(zipEntry);
|
|
InputStream is = zipFile.getInputStream(zipEntry);
|
|
FileOutputStream fos = new FileOutputStream(targetFile);
|
|
FileOutputStream fos = new FileOutputStream(targetFile);
|
|
int len;
|
|
int len;
|
|
- byte[] buf = new byte[8192];
|
|
|
|
|
|
+ byte[] buf = new byte[2048];
|
|
while ((len = is.read(buf)) != -1) {
|
|
while ((len = is.read(buf)) != -1) {
|
|
fos.write(buf, 0, len);
|
|
fos.write(buf, 0, len);
|
|
}
|
|
}
|
|
- urlList.add(url);
|
|
|
|
// 关流顺序,先打开的后关闭
|
|
// 关流顺序,先打开的后关闭
|
|
fos.close();
|
|
fos.close();
|
|
is.close();
|
|
is.close();
|
|
|
|
+ urlList.add(url);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- deletelocalFiles(path.concat("oldZip/").concat(fileUpload.getCfileName()));
|
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- log.error("解压原zip失败 ", e);
|
|
|
|
|
|
+ log.error("解压原zip文件失败 ", e);
|
|
}
|
|
}
|
|
|
|
+ deletelocalFiles(innerZipPath);
|
|
return urlList;
|
|
return urlList;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -257,8 +262,8 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
|
|
* @throws
|
|
* @throws
|
|
* @author ZHAOXA
|
|
* @author ZHAOXA
|
|
*/
|
|
*/
|
|
- private ShiwanFileUpload createNewZipFiles(List<String> list, String innerFilePath, String zipfilename, String updateName, String userId, Long accountId, String fileType) throws IOException {
|
|
|
|
- String zipUrl = FileUtil.listToZip(list, innerFilePath, zipfilename);
|
|
|
|
|
|
+ private ShiwanFileUpload createNewZipFiles(List<String> list, String path, String zipfilename, String updateName, String userId, Long accountId, String fileType) throws IOException {
|
|
|
|
+ String zipUrl = FileUtil.zipCompress(path.concat(zipfilename), new File(path.concat("innerFiles/")));
|
|
ShiwanFileUpload shiwanFileUpload = new ShiwanFileUpload();
|
|
ShiwanFileUpload shiwanFileUpload = new ShiwanFileUpload();
|
|
shiwanFileUpload.setUserId(userId);
|
|
shiwanFileUpload.setUserId(userId);
|
|
shiwanFileUpload.setAccountId(accountId);
|
|
shiwanFileUpload.setAccountId(accountId);
|