Răsfoiți Sursa

文件上传接口

yonghuifan 1 an în urmă
părinte
comite
16fe2d4e9f

+ 5 - 44
src/main/java/com/dragon/tj/portal/controller/FileManageController.java

@@ -1,65 +1,26 @@
 package com.dragon.tj.portal.controller;
 
 import com.dragon.tj.portal.common.base.R;
-import com.dragon.tj.portal.common.util.UploadUtils;
+import com.dragon.tj.portal.service.FileManageService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.ApplicationContext;
 import org.springframework.web.bind.annotation.PathVariable;
 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 java.io.File;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.Map;
 
 @RestController
-@RequestMapping("/file")
+@RequestMapping("/fileMge")
 public class FileManageController {
 
-    @Value("${dragon.file.path}")
-    private String localPath;
-
     @Autowired
-    private ApplicationContext applicationContext;
+    private FileManageService fileManageService;
 
     @PostMapping("/upload/{type}")
-    public R upload(MultipartFile file, @PathVariable("type")String type){
-
-        String originalFilename = file.getOriginalFilename();
-        System.out.println(originalFilename);
-
-        String uuidFilename = UploadUtils.getUUIDName(originalFilename);
-
-        File dateDir = new File(localPath + type);
-        if (!dateDir.exists()) {
-            dateDir.mkdirs();
-        }
-
-        File newFile = new File(dateDir.getPath(),uuidFilename);
-
-        try {
-            file.transferTo(newFile);
-        } catch (IOException e) {
-            e.printStackTrace();
-            return R.failed().setMsg(e.getMessage());
-        }
-
-        Map<String,String> res = new HashMap<>();
-        try {
-            String hostAddress = InetAddress.getLocalHost().getHostAddress();
-            String port = applicationContext.getEnvironment().getProperty("server.port");
-            res.put("url","http://"+ hostAddress + ":" + port + "/file/" + type + "/" +uuidFilename);
-        } catch (UnknownHostException e) {
-            e.printStackTrace();
-        }
+    public R upload(MultipartFile file, @PathVariable("type")Integer type){
 
-        return R.ok().setData(res).setMsg("okk");
+        return R.ok().setData(fileManageService.upload(file,type));
     }
 
 }

+ 38 - 0
src/main/java/com/dragon/tj/portal/entity/FileInfo.java

@@ -0,0 +1,38 @@
+package com.dragon.tj.portal.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+
+import java.time.LocalDateTime;
+
+/*
+* 文件
+* */
+@Getter
+@Setter
+@Accessors(chain = true)
+@TableName("file_info")
+public class FileInfo {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;//主键
+    private String fileName;//文件名
+    private String url;//路径
+    private Integer type;//文件类型(办公常用,系统工具......)
+    private String extension;//拓展名
+    private Long size;//文件大小
+    private Long downloads;//下载量
+    private LocalDateTime createTime;//上传时间
+    private LocalDateTime updateTime;//更新时间
+    private String delFlag;//删除
+    private String createUser;//上传人
+    private String contentType;//ContentType
+
+
+}

+ 13 - 0
src/main/java/com/dragon/tj/portal/mapper/FileManageMapper.java

@@ -0,0 +1,13 @@
+package com.dragon.tj.portal.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.dragon.tj.portal.entity.FileInfo;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 应用程序
+ */
+@Mapper
+public interface FileManageMapper extends BaseMapper<FileInfo> {
+
+}

+ 8 - 0
src/main/java/com/dragon/tj/portal/service/FileManageService.java

@@ -0,0 +1,8 @@
+package com.dragon.tj.portal.service;
+
+import com.dragon.tj.portal.entity.FileInfo;
+import org.springframework.web.multipart.MultipartFile;
+
+public interface FileManageService {
+    FileInfo upload(MultipartFile file,Integer type);
+}

+ 80 - 0
src/main/java/com/dragon/tj/portal/service/impl/FileManageServiceImpl.java

@@ -0,0 +1,80 @@
+package com.dragon.tj.portal.service.impl;
+
+import com.dragon.tj.portal.auth.model.LoginUser;
+import com.dragon.tj.portal.auth.util.SecurityUtils;
+import com.dragon.tj.portal.common.util.UploadUtils;
+import com.dragon.tj.portal.entity.FileInfo;
+import com.dragon.tj.portal.mapper.FileManageMapper;
+import com.dragon.tj.portal.service.FileManageService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationContext;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class FileManageServiceImpl implements FileManageService {
+
+    @Value("${dragon.file.path}")
+    private String localPath;
+
+    @Autowired
+    private ApplicationContext applicationContext;
+
+    @Autowired
+    private FileManageMapper fileManageMapper;
+
+    @Override
+    public FileInfo upload(MultipartFile file,Integer type) {
+        String originalFilename = file.getOriginalFilename();
+        System.out.println(originalFilename);
+
+        String uuidFilename = UploadUtils.getUUIDName(originalFilename);
+
+        File dateDir = new File(localPath + type);
+        if (!dateDir.exists()) {
+            dateDir.mkdirs();
+        }
+
+        File newFile = new File(dateDir.getPath(),uuidFilename);
+
+        try {
+            file.transferTo(newFile);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+
+        Map<String,String> res = new HashMap<>();
+        FileInfo fileInfo = new FileInfo();
+        String[] split = originalFilename.split("\\.");
+        fileInfo.setFileName(split[0]);
+        fileInfo.setExtension(split[1]);
+        fileInfo.setSize(file.getSize());
+        fileInfo.setType(type);
+        fileInfo.setContentType(file.getContentType());
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        fileInfo.setCreateUser(loginUser.getUsername());
+
+        try {
+            String hostAddress = InetAddress.getLocalHost().getHostAddress();
+            String port = applicationContext.getEnvironment().getProperty("server.port");
+            String url = "http://"+ hostAddress + ":" + port + "/file/" + type + "/" +uuidFilename;
+            fileInfo.setUrl(url);
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        }
+
+        //save to db
+        fileManageMapper.insert(fileInfo);
+
+        return fileInfo;
+    }
+}