package com.hxzk.gps.controller.UploadFile;
|
|
import cn.dev33.satoken.annotation.SaCheckLogin;
|
import com.hxzk.gps.controller.UploadFile.deo.FileUpLoad;
|
import com.hxzk.gps.controller.UploadFile.deo.ReturnMessageUpload;
|
|
import com.hxzk.gps.util.MessageUtils.MessageUtils;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.nio.file.StandardCopyOption;
|
import java.util.UUID;
|
|
/*
|
* 上传照片
|
* @author YuZhiTong
|
* @since 2025-05-19
|
*
|
* */
|
@RestController
|
@RequestMapping("/UploadFile")
|
public class UploadPic {
|
|
@Value("${upload.dir-Person}")
|
private String uploadDirPerson;
|
|
@Value("${upload.dir-DepartMent}")
|
private String uploadDirDepartMent;
|
|
@Value("${upload.dir-IP}")
|
private String uploadIp;
|
|
@Value("${upload.dir-CompanyLogo}")
|
private String uploadDirCompanyLogo;
|
|
@Value("${upload.dir-Hksxt}")
|
private String uploadDirHksxtIcon;
|
@Value("${upload.dir-UserImg}")
|
private String UserImg;
|
|
/*
|
* 公司Logo上传
|
* */
|
@SaCheckLogin
|
@PostMapping("/uploadUserImg")
|
public ReturnMessageUpload uploadCompanyLogo(@RequestParam("file") MultipartFile file) {
|
if (file.isEmpty()) {
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
try {
|
// 生成唯一的文件名,避免文件名冲突
|
String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
|
// 构建文件保存路径
|
Path filePath = Paths.get(UserImg, fileName);
|
// 检查上传目录是否存在,如果不存在则创建
|
File dir = new File(UserImg);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
// 将上传的文件保存到指定路径
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
FileUpLoad fileUpLoad = new FileUpLoad();
|
fileUpLoad.setFileUrl(uploadIp+"User/"+fileName);
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
|
} catch (IOException e) {
|
e.printStackTrace();
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
}
|
|
|
/*
|
* 终端照片上传
|
* */
|
@SaCheckLogin
|
@PostMapping("/uploadPersonPic")
|
public ReturnMessageUpload uploadPersonPic(@RequestParam("file") MultipartFile file) {
|
if (file.isEmpty()) {
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
try {
|
// 生成唯一的文件名,避免文件名冲突
|
String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
|
// 构建文件保存路径
|
Path filePath = Paths.get(uploadDirPerson, fileName);
|
// 检查上传目录是否存在,如果不存在则创建
|
File dir = new File(uploadDirPerson);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
// 将上传的文件保存到指定路径
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
FileUpLoad fileUpLoad = new FileUpLoad();
|
fileUpLoad.setFileUrl(uploadIp+"Person/"+fileName);
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
|
} catch (IOException e) {
|
e.printStackTrace();
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
}
|
|
|
|
/*
|
* 部门图标上传
|
* */
|
@SaCheckLogin
|
@PostMapping("/uploadDepartMentIconPic")
|
public ReturnMessageUpload uploadDepartMentIconPic(@RequestParam("file") MultipartFile file) {
|
if (file.isEmpty()) {
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
try {
|
// 生成唯一的文件名,避免文件名冲突
|
String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
|
// 构建文件保存路径
|
Path filePath = Paths.get(uploadDirDepartMent, fileName);
|
// 检查上传目录是否存在,如果不存在则创建
|
File dir = new File(uploadDirDepartMent);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
// 将上传的文件保存到指定路径
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
FileUpLoad fileUpLoad = new FileUpLoad();
|
fileUpLoad.setFileUrl(uploadIp+"DepartMent/"+fileName);
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
|
} catch (IOException e) {
|
e.printStackTrace();
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
}
|
|
/*
|
* 公司Logo上传
|
* */
|
@SaCheckLogin
|
@PostMapping("/uploadCompanyLogo")
|
public ReturnMessageUpload uploadUserImg(@RequestParam("file") MultipartFile file) {
|
if (file.isEmpty()) {
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
try {
|
// 生成唯一的文件名,避免文件名冲突
|
String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
|
// 构建文件保存路径
|
Path filePath = Paths.get(uploadDirCompanyLogo, fileName);
|
// 检查上传目录是否存在,如果不存在则创建
|
File dir = new File(uploadDirCompanyLogo);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
// 将上传的文件保存到指定路径
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
FileUpLoad fileUpLoad = new FileUpLoad();
|
fileUpLoad.setFileUrl(uploadIp+"CompanyLogo/"+fileName);
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
|
} catch (IOException e) {
|
e.printStackTrace();
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
}
|
|
/*
|
* 监控图标上传
|
* */
|
@SaCheckLogin
|
@PostMapping("/uploadHksxtIcon")
|
public ReturnMessageUpload uploadHksxtIcon(@RequestParam("file") MultipartFile file) {
|
if (file.isEmpty()) {
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
try {
|
// 生成唯一的文件名,避免文件名冲突
|
String fileName = UUID.randomUUID().toString() + "." + getFileExtension(file.getOriginalFilename());
|
// 构建文件保存路径
|
Path filePath = Paths.get(uploadDirHksxtIcon, fileName);
|
// 检查上传目录是否存在,如果不存在则创建
|
File dir = new File(uploadDirHksxtIcon);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
// 将上传的文件保存到指定路径
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
FileUpLoad fileUpLoad = new FileUpLoad();
|
fileUpLoad.setFileUrl(uploadIp+"Hksxt/"+fileName);
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("SuccessPicture"), fileUpLoad);
|
} catch (IOException e) {
|
e.printStackTrace();
|
return new ReturnMessageUpload(200, MessageUtils.getMessage("ErrorPicture"), null);
|
}
|
}
|
|
private String getFileExtension(String fileName) {
|
if (fileName != null && fileName.lastIndexOf(".") != -1) {
|
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
}
|
return "";
|
}
|
}
|