文件上传

This commit is contained in:
cwchen 2025-02-19 14:00:31 +08:00
parent 835d70b126
commit 45593bceb0
7 changed files with 277 additions and 2 deletions

View File

@ -0,0 +1,55 @@
package com.bonus.system.file.controller;
import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.system.file.util.IDUtils;
import com.bonus.system.file.util.SystemUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.util.HashMap;
import java.util.Map;
/**
* @className:FileUploadController
* @author:cwchen
* @date:2025-02-19-13:36
* @version:1.0
* @description:文件上传
*/
@RestController
@RequestMapping("/fileUpload/")
@Slf4j
public class FileUploadController {
@ApiOperation(value = "单文件上传")
@PostMapping("singleUploadFile")
public AjaxResult singleUploadFile(@RequestParam(value = "file") MultipartFile file){
try {
String fileName = file.getOriginalFilename();
String suffix= IDUtils.getSuffix(fileName);
String path="/" + DateTimeHelper.getNowYmd()+"/"+ IDUtils.createID()+suffix;
String newPath= SystemUtils.getUploadPath()+path;
File uploadFile = new File(newPath);
//生成文件夹
if (!uploadFile.getParentFile().exists()) {
uploadFile.getParentFile().mkdirs();
}
// 存入临时文件
file.transferTo(uploadFile);
Map<String, String> map = new HashMap<>();
map.put("filePath",path);
map.put("fileName",fileName);
return AjaxResult.success("上传成功",map);
}catch (Exception e){
log.error(e.toString(),e);
return AjaxResult.error("文件上传异常");
}
}
}

View File

@ -0,0 +1,51 @@
package com.bonus.system.file.entity;
import lombok.Data;
/**
* @className:FileSourceVo
* @author:cwchen
* @date:2025-02-19-11:20
* @version:1.0
* @description:资源文件-vo
*/
@Data
public class FileSourceVo {
/**
* id
*/
private Long id;
/**
* 文件名称
*/
private String fileName;
/**
* 文件后缀
*/
private String fileSuffix;
/**
* 文件路径
*/
private String filePath;
/**
* 文件类型 1图片 2文件
*/
private String fileType;
/**
* 资源id
*/
private String sourceId;
/**
* 资源类型(码表进行配置)
*/
private String sourceType;
/**
* 创建人
*/
private Long createUser;
/**
* 更新人
*/
private Long updateUser;
}

View File

@ -0,0 +1,11 @@
package com.bonus.system.file.service;
/**
* @className:FileUploadService
* @author:cwchen
* @date:2025-02-19-13:39
* @version:1.0
* @description:文件上传
*/
public class FileUploadService {
}

View File

@ -0,0 +1,26 @@
package com.bonus.system.file.service;
import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.system.file.util.IDUtils;
import com.bonus.system.file.util.SystemUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @className:FileUploadService
* @author:cwchen
* @date:2025-02-19-11:30
* @version:1.0
* @description:文件上传-业务层
*/
@Service(value = "FileUploadService")
public class FileUploadServiceImpl {
}

View File

@ -0,0 +1,52 @@
package com.bonus.system.file.util;
public class IDUtils {
private static byte[] lock = new byte[0];
// 位数默认是8位
private final static long w = 100000000;
/**
* 传入文件 名称
* @param fileName
* @return
*/
public static String getSuffix(String fileName){
return fileName.substring(fileName.lastIndexOf('.'));
}
public static void main(String[] args) {
System.err.println(createID());
}
/**
* 获取id
* @return
*/
public static String createID() {
long r = 0;
synchronized (lock) {
r = (long) ((Math.random() + 1) * w);
}
return System.currentTimeMillis() + String.valueOf(r).substring(1);
}
/**
* 是否是文件/图片
* @param name
* @return
*/
public static String isImageFileExtension(String name) {
name=name.toLowerCase();
String[] imageFormats = {"jpg", "jpeg", "png", "gif", "bmp", "tiff"};
for (String format : imageFormats) {
if (format.equals(name)) { // 判断扩展名
return "1";
}
}
return "2"; // 不是支持的图片格式
}
}

View File

@ -0,0 +1,73 @@
package com.bonus.system.file.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class SystemUtils {
public static String windowsPath;
public static String linuxPath;
/**
* 自动注入
*
* @param windowsPath
*/
@Value("${file.upload_path.windows}")
public void setWindowsPath(String windowsPath) {
SystemUtils.windowsPath = windowsPath;
}
/**
* 自动注入
*
* @param linuxPath
*/
@Value("${file.upload_path.linux}")
public void setLinuxPath(String linuxPath) {
SystemUtils.linuxPath = linuxPath;
}
/**
* 返回系统
*
* @return
*/
public static String getSystem() {
String os = System.getProperty("os.name");
//Windows操作系统
if (os != null && os.toLowerCase().startsWith("windows")) {
return "windows";
} else if (os != null && os.toLowerCase().startsWith("linux")) {//Linux操作系统
return "linux";
} else { //其它操作系统
return "other";
}
}
/**
* 获取文件上传路径
*
* @return
*/
public static String getUploadPath() {
String os = getSystem();
System.err.println("当前系统是=" + os);
if ("windows".equals(os)) {
return windowsPath;
} else if ("linux".equals(os)) {
return linuxPath;
} else {
return windowsPath;
}
}
}

View File

@ -31,7 +31,7 @@
FROM sys_dict_data FROM sys_dict_data
where dict_type = 'att_status' and is_leave = '1')) where dict_type = 'att_status' and is_leave = '1'))
</if> </if>
<if test='bean.attStatus != null and bean.attStatus != "6"'> <if test='bean.attStatus != null and bean.attStatus != "6" and bean.attStatus!="27"'>
<if test='bean.attStatus == "1"'> <if test='bean.attStatus == "1"'>
and ( toWorkAttStatus = #{bean.attStatus} and offWorkAttStatus = #{bean.attStatus} ) and ( toWorkAttStatus = #{bean.attStatus} and offWorkAttStatus = #{bean.attStatus} )
</if> </if>
@ -39,7 +39,14 @@
and ( toWorkAttStatus = #{bean.attStatus} or offWorkAttStatus = #{bean.attStatus} ) and ( toWorkAttStatus = #{bean.attStatus} or offWorkAttStatus = #{bean.attStatus} )
</if> </if>
</if> </if>
<if test='bean.attStatus != null and bean.attStatus == "27"'>
and (v.toWorkAttStatus in (SELECT dict_value
FROM sys_dict_data
where dict_type = 'att_status' and is_leave IN ('1','2')) or v.offWorkAttStatus in (SELECT dict_value
FROM sys_dict_data
where dict_type = 'att_status' and is_leave IN ('1','2')))
AND (v.offWorkAttCurrentTime IS NOT NULL OR v.toWorkAttCurrentTime IS NOT NULL)
</if>
<if test='bean.orgList != null and bean.orgList.size() > 0'> <if test='bean.orgList != null and bean.orgList.size() > 0'>
and v.org_id in ( and v.org_id in (
<foreach collection="bean.orgList" item="item" separator=","> <foreach collection="bean.orgList" item="item" separator=",">