|
@@ -1,7 +1,12 @@
|
|
|
package com.museum.web.controller;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.URLUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
+import com.museum.common.util.FileUtils;
|
|
|
import com.museum.common.util.Result;
|
|
|
import com.museum.domain.entity.FileEntity;
|
|
|
import com.museum.domain.entity.DownloadEntity;
|
|
@@ -27,6 +32,11 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
@@ -132,4 +142,48 @@ public class DownloadController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
|
+ @ApiOperation("下载")
|
|
|
+ @GetMapping("download/{id}")
|
|
|
+ public Result download(@PathVariable Long id) throws IOException {
|
|
|
+ DownloadResponse entity = downloadService.rFindById(id);
|
|
|
+ if (entity == null) {
|
|
|
+ log.error("对象不存在: {}", id);
|
|
|
+ return Result.failure("对象不存在");
|
|
|
+ }
|
|
|
+ String fileIds = entity.getFileIds();
|
|
|
+ List<FileEntity> files = StringUtils.isNotBlank(fileIds) ? fileService.findByIds(fileIds) : new ArrayList<>();
|
|
|
+
|
|
|
+ // 1.将oss资源下载到服务器
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+
|
|
|
+ // 保存目录
|
|
|
+ String serverPath = OUT_PATH + time +"/";
|
|
|
+ FileUtil.mkdir(serverPath);
|
|
|
+ for (FileEntity dbFile : files) {
|
|
|
+
|
|
|
+ FileUtils.fielWrite(dbFile.getFilePath(), serverPath + dbFile.getFileName());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 2.将下载文件压缩zip
|
|
|
+ log.info("zipFile: {}", OUT_PATH + time+".zip");
|
|
|
+ ZipUtil.zip(serverPath, OUT_PATH + time+".zip");
|
|
|
+
|
|
|
+
|
|
|
+ // 3.返回静态资源路径(需要tomcat配置静态资源路径)
|
|
|
+ String domainUrl = SERVER_DOMAIN + time + ".zip";
|
|
|
+ log.info("domainUrl: {}", domainUrl);
|
|
|
+
|
|
|
+ return Result.success(domainUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws MalformedURLException {
|
|
|
+
|
|
|
+// InputStream stream = URLUtil.getStream(new URL("http://4dkankan.oss-cn-shenzhen.aliyuncs.com/sh_museum/image/20200609_114844672_0.jpg"));
|
|
|
+// FileUtil.writeFromStream(stream, "F:\\test\\sh_museum_cms\\aa1.jpg");
|
|
|
+ ZipUtil.zip("F:\\test\\sh_museum_cms\\20200512_175734309\\", "F:\\test\\sh_museum_cms\\20200512_175734309.zip");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|