|
@@ -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();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fileManageMapper.insert(fileInfo);
|
|
|
+
|
|
|
+ return fileInfo;
|
|
|
+ }
|
|
|
+}
|