添加互联网岗位上传附件,未作批量处理(job)表
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
package com.ruoyi.cms.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.cms.mapper.FileJobUploadMapper;
|
||||
import com.ruoyi.cms.service.IFileJobUploadService;
|
||||
import com.ruoyi.cms.util.StringUtil;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.entity.FileJobUpload;
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件Service业务层处理
|
||||
*
|
||||
* @author ${author}
|
||||
* @date 2025-03-18
|
||||
*/
|
||||
@Service
|
||||
public class FileJobUploadServiceImpl extends ServiceImpl<FileJobUploadMapper, FileJobUpload> implements IFileJobUploadService
|
||||
{
|
||||
@Autowired
|
||||
private FileJobUploadMapper fileJobUploadMapper;
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
/**
|
||||
* 查询文件
|
||||
*
|
||||
* @param id 文件主键
|
||||
* @return 文件
|
||||
*/
|
||||
@Override
|
||||
public FileJobUpload selectFileById(Long id)
|
||||
{
|
||||
return fileJobUploadMapper.selectById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文件列表
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 文件
|
||||
*/
|
||||
@Override
|
||||
public List<FileJobUpload> selectFileList(FileJobUpload file)
|
||||
{
|
||||
return fileJobUploadMapper.selectFileList(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertFile(FileJobUpload file)
|
||||
{
|
||||
return fileJobUploadMapper.insert(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateFile(FileJobUpload file)
|
||||
{
|
||||
return fileJobUploadMapper.updateById(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除文件
|
||||
*
|
||||
* @param ids 需要删除的文件主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteFileByIds(Long[] ids)
|
||||
{
|
||||
return fileJobUploadMapper.deleteBatchIds(Arrays.asList(ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult upload(MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件为空,请选择文件上传");
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建上传目录
|
||||
java.io.File dir = new java.io.File(uploadDir).getCanonicalFile();
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 生成唯一的文件名
|
||||
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
|
||||
|
||||
Path basePath = Paths.get(getCanonicalPath(uploadDir)).normalize();
|
||||
Path filePath = basePath.resolve(fileName).normalize();
|
||||
|
||||
if (!filePath.startsWith(basePath)) {
|
||||
throw new SecurityException("非法路径,禁止访问");
|
||||
}
|
||||
|
||||
// 保存文件到服务器
|
||||
Files.copy(file.getInputStream(), filePath);
|
||||
|
||||
java.io.File destFile = filePath.toFile();
|
||||
destFile.setReadable(true, false); // 所有用户可读
|
||||
destFile.setWritable(false); // 禁止写入
|
||||
destFile.setExecutable(false);
|
||||
|
||||
// 保存文件信息到数据库
|
||||
saveFileInfo(fileName);
|
||||
|
||||
return AjaxResult.success(StringUtil.PATH_PROXY_50+fileName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return AjaxResult.error("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加返回bussinessid的接口,新增时使用
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public AjaxResult uploadFile(MultipartFile file, HttpServletRequest request) {
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件为空,请选择文件上传");
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建上传目录
|
||||
java.io.File dir = new java.io.File(uploadDir).getCanonicalFile();
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 生成唯一的文件名
|
||||
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
|
||||
|
||||
Path basePath = Paths.get(getCanonicalPath(uploadDir)).normalize();
|
||||
Path filePath = basePath.resolve(fileName).normalize();
|
||||
|
||||
if (!filePath.startsWith(basePath)) {
|
||||
throw new SecurityException("非法路径,禁止访问");
|
||||
}
|
||||
|
||||
// 保存文件到服务器
|
||||
Files.copy(file.getInputStream(), filePath);
|
||||
|
||||
java.io.File destFile = filePath.toFile();
|
||||
destFile.setReadable(true, false); // 所有用户可读
|
||||
destFile.setWritable(false); // 禁止写入
|
||||
destFile.setExecutable(false);
|
||||
|
||||
// 保存文件信息到数据库
|
||||
FileJobUpload svFile=saveFileInfo(fileName);
|
||||
AjaxResult ajaxResult=AjaxResult.success();
|
||||
ajaxResult.put("filePath", StringUtil.getFilePath(request)+fileName);
|
||||
ajaxResult.put("id",String.valueOf(svFile.getId()));
|
||||
return ajaxResult;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return AjaxResult.error("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
private FileJobUpload saveFileInfo(String fileName) {
|
||||
// 这里假设你已经有了一个FileService来处理数据库操作
|
||||
FileJobUpload file = new FileJobUpload();
|
||||
file.setFileUrl(fileName);
|
||||
this.save(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
private String getCanonicalPath(String path) {
|
||||
try {
|
||||
return new java.io.File(path).getCanonicalPath();
|
||||
} catch (Exception e) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user