|
@@ -0,0 +1,146 @@
|
|
|
+package com.gis.web.controller;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.gis.common.util.AliyunOssUtil;
|
|
|
+import com.gis.common.util.FileUtils;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.domain.entity.FileEntity;
|
|
|
+import com.gis.service.FileService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import springfox.documentation.annotations.ApiIgnore;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/5/9 0018 12:17
|
|
|
+ */
|
|
|
+@Log4j2
|
|
|
+@Api(tags = "文件服务")
|
|
|
+@RestController
|
|
|
+@RequestMapping("manage/file")
|
|
|
+public class FileController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 到时后商量是否全部图片都要生成缩略图
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @ApiOperation("上传")
|
|
|
+ @PostMapping(value = "upload", consumes = {"multipart/form-data"})
|
|
|
+ public Result upload(MultipartFile file ) throws IOException {
|
|
|
+
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return Result.failure("文件不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入本地服务器
|
|
|
+ HashMap<String, String> fileInfo = FileUtils.upload(file, FILE_PATH);
|
|
|
+
|
|
|
+
|
|
|
+ // 保存db
|
|
|
+ FileEntity entity = new FileEntity();
|
|
|
+ entity.setFileName(fileInfo.get("name"));
|
|
|
+
|
|
|
+ String filePath = fileInfo.get("path");
|
|
|
+ entity.setFilePath(filePath);
|
|
|
+
|
|
|
+ String newName = fileInfo.get("newName");
|
|
|
+ String urlPath = SERVER_DOMAIN+"data/" + newName;
|
|
|
+ entity.setUrlPath(urlPath);
|
|
|
+
|
|
|
+ // 生成缩略图
|
|
|
+ String thumbPath = FILE_PATH + "thumb_" + newName;
|
|
|
+ createThumb(filePath, thumbPath);
|
|
|
+
|
|
|
+ String thumbUrl = SERVER_DOMAIN+"data/thumb_" + newName;
|
|
|
+ entity.setThumb(thumbUrl);
|
|
|
+
|
|
|
+ fileService.save(entity);
|
|
|
+
|
|
|
+ // 返回前端数据
|
|
|
+ return Result.success(entity);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @ApiIgnore
|
|
|
+ @ApiOperation("上传, 可以接收多个文件")
|
|
|
+ @PostMapping(value = "uploads", consumes = {"multipart/form-data"})
|
|
|
+ public Result uploads(MultipartFile [] file ) throws IOException {
|
|
|
+
|
|
|
+ if (file.length <= 0) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return Result.failure("文件不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入本地服务器
|
|
|
+ List<Map<String, String>> uploads = FileUtils.uploads(file, FILE_PATH);
|
|
|
+
|
|
|
+ HashMap<String, String> resultMap = new HashMap<>();
|
|
|
+ String dir = null;
|
|
|
+ for (Map<String, String> map: uploads) {
|
|
|
+ String fileName = map.get("name");
|
|
|
+ String newName = map.get("newName");
|
|
|
+ String path = FILE_PATH+"/" + newName;
|
|
|
+ String filePath = map.get("path");
|
|
|
+ log.info("local {}, ", filePath);
|
|
|
+ // urlPath
|
|
|
+ dir = map.get("dir");
|
|
|
+ String urlPath = SERVER_DOMAIN+"data/"+dir+"/"+newName;
|
|
|
+
|
|
|
+ // 保存db
|
|
|
+ FileEntity entity = new FileEntity();
|
|
|
+ entity.setFileName(fileName);
|
|
|
+// String ossUrl = OSS_DOMAIN+ossPath;
|
|
|
+ entity.setFilePath(filePath);
|
|
|
+ entity.setUrlPath(urlPath);
|
|
|
+
|
|
|
+ // 创建缩略图
|
|
|
+
|
|
|
+ fileService.save(entity);
|
|
|
+
|
|
|
+ // 返回前端数据
|
|
|
+ resultMap.put(entity.getId().toString(), entity.getUrlPath());
|
|
|
+// dir = map.get("dir");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除本地服务器物理文件,用删除目录的方式
|
|
|
+// FileUtil.del(OUT_PATH + dir);
|
|
|
+
|
|
|
+ return Result.success(resultMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建缩略图
|
|
|
+ * @param filePath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String createThumb(String filePath, String savePath){
|
|
|
+ ImgUtil.scale(FileUtil.file(filePath), FileUtil.file(savePath), 0.2f);
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|