This commit is contained in:
ybb 2025-08-04 09:47:49 +08:00
parent c517e99956
commit 9c8cff069c
17 changed files with 3119 additions and 6 deletions

2327
ceshi/test.sql Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
package com.dite.znpt.domain.dto;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("接受文件夹参数")
public class FolderDto {
private String name = "tom";
private Long parentId = 0L;
}

View File

@ -0,0 +1,38 @@
package com.dite.znpt.domain.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("business_data_part")
@ApiModel(value="商务资料文件夹对象")
@AllArgsConstructor
@NoArgsConstructor
public class BusinessDataEntity {
// 主键
private Long folderId = null;
// 文件夹名称
private String folderName = null;
// 父级文件夹
private Long parentId = null;
// 创建人
private Long creatorId = null;
// 创建时间
private LocalDateTime createTime = null;
// 更新时间
private LocalDateTime updateTime = null;
// 是否删除
private Boolean isDeleted = false;
// 文件夹路径
private String folderPath = null;
}

View File

@ -0,0 +1,38 @@
package com.dite.znpt.domain.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("business_data_part_file")
@ApiModel(value="商务资料对象")
public class BusinessDataFileEntity {
//文件id
private Long fileId = null;
//文件夹id
private Long folderId = null;
//文件名
private String fileName = null;
//文件路径
private String filePath = null;
//文件类型
private String fileType = "unknown";
//文件大小
private Long fileSize = null;
//上传时间
private Date uploadTime = null;
//上传人id
private Long uploaderId = null;
//是否删除
private Boolean isDeleted = false;
}

View File

@ -0,0 +1,16 @@
package com.dite.znpt.domain.page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private Long total;
private List<?> rows;
}

View File

@ -0,0 +1,20 @@
package com.dite.znpt.mapper;
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@ApiOperation("商务资料文件对象")
public interface BusinessDataFileMapper {
public List<BusinessDataFileEntity> List( @Param("folderId") Long folderId,String fileName);
void delete(@Param("fileId") Long fileId,@Param("folderId") Long folderId);
void add(BusinessDataFileEntity businessDataFileEntity);
String getPath(Long fileId);
}

View File

@ -0,0 +1,29 @@
package com.dite.znpt.mapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import java.time.LocalDateTime;
import java.util.List;
@ApiOperation("商务资料文件夹对象")
public interface BusinessDataMapper {
public String getPath(Long parentId);
public List<BusinessDataEntity> List();
void insert(BusinessDataEntity businessDataEntity);
void delete(Long folderId);
void reName(BusinessDataEntity businessDataEntity);
}

View File

@ -0,0 +1,23 @@
package com.dite.znpt.service;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import com.dite.znpt.domain.page.PageBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@ApiOperation("商务资料文件service")
public interface BusinessDataFileService {
PageBean pageSelect(Integer page, Integer pageSize, Long folderId);
Result delete(@RequestParam(value = "fileId", required = false) Long fileId,@RequestParam(value = "foldelId", required = false) Long folderId);
void add(BusinessDataFileEntity businessDataFileEntity);
String getPath(Long fileId);
}

View File

@ -0,0 +1,19 @@
package com.dite.znpt.service;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.page.PageBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import java.util.List;
@ApiOperation("商务资料文件夹service")
public interface BusinessDataService {
PageBean pageSelect(Integer page, Integer pageSize);
Result createFolder(String folderName, Long parentId);
String getPath(Long parentId);
Result delete(Long folderId);
Result reName(Long folderId, String newName);
}

View File

@ -0,0 +1,95 @@
package com.dite.znpt.service.impl;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import com.dite.znpt.domain.page.PageBean;
import com.dite.znpt.mapper.BusinessDataFileMapper;
import com.dite.znpt.service.BusinessDataFileService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static jodd.io.FileUtil.deleteFile;
import static org.apache.tomcat.util.http.fileupload.FileUtils.deleteDirectory;
@AllArgsConstructor
@Service
@ApiOperation("商务资料文件service实现类")
public class BusinessDataFileServiceImpl implements BusinessDataFileService {
@Resource
private BusinessDataFileMapper businessDataFileMapper;
@ApiOperation("分页查询文件")
public PageBean pageSelect(Integer page, Integer pageSize, Long folderId) {
PageHelper.startPage(page, pageSize);
List<BusinessDataFileEntity> list = businessDataFileMapper.List(folderId ,"");
Page<BusinessDataFileEntity> p= (Page<BusinessDataFileEntity>) list;
PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
return pageBean;
}
@ApiOperation("删除文件")
public Result delete(Long fileId, Long folderId) {
//删除数据库数据
if (folderId != null){
businessDataFileMapper.delete(null,folderId);
System.out.println("888888888走对了");
return Result.okM("删除,走对了,成功");
}
//删除文件
String sPath = businessDataFileMapper.getPath(fileId);
businessDataFileMapper.delete(fileId,null);
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return Result.error("文件不存在");
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
try {
deleteFile(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Result.okM("删除成功");
} else { // 为目录时调用删除目录方法
try {
deleteDirectory(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Result.okM("删除成功");
}
}
}
@ApiOperation("增加文件")
public void add(BusinessDataFileEntity businessDataFileEntity) {
businessDataFileMapper.add(businessDataFileEntity);
}
@ApiOperation("获取文件路径")
public String getPath(Long fileId) {
System.out.println(fileId+" 77777777777impl777777777777777");
return businessDataFileMapper.getPath(fileId);
}
}

View File

@ -0,0 +1,207 @@
package com.dite.znpt.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.page.PageBean;
import com.dite.znpt.mapper.BusinessDataFileMapper;
import com.dite.znpt.mapper.BusinessDataMapper;
import com.dite.znpt.service.BusinessDataFileService;
import com.dite.znpt.service.BusinessDataService;
import com.dite.znpt.domain.entity.BusinessDataEntity;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.ibatis.jdbc.Null;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.aspectj.bridge.IMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
/**
* 商务资料文件夹实现类
*/
@Service
@ApiOperation("商务资料文件夹service实现类")
@AllArgsConstructor
@NoArgsConstructor
public class BusinessDataServiceImpl implements BusinessDataService {
@Resource
private BusinessDataMapper businessDataMapper;
@Resource
private BusinessDataFileService businessDataFileService;
// 从配置文件中读取基础路径默认值D:/upload/businessData
// ,新建文件夹的时候如果没指定父文件夹Id,就用这个
@Value("${file.upload.businessDataPath:D:/upload/businessData}")
private String businessDataPath;
@ApiOperation(value = "分页查询")
@Override
public PageBean pageSelect(Integer page, Integer pageSize) {
// 分页查询,pageHelper.startPage()方法会返回一个Page对象该对象包含了分页信息如总记录数和当前页数据
PageHelper.startPage(page, pageSize);
// 查询所有数据
List<BusinessDataEntity> businessDataEntityList = businessDataMapper.List();
// 返回分页结果
Page<BusinessDataEntity> p = (Page<BusinessDataEntity>) businessDataEntityList;
// 返回分页结果
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
@ApiOperation(value = "创建文件夹")
@Override
public Result createFolder(String folderName, Long parentId) {
//获取ID
Long loginIdAsLong = 888L;
// loginIdAsLong = StpUtil.getLoginIdAsLong();
//
// 文件夹名称不能为空
//TODO: 添加文件夹名称校验,后续最好更规范些写个工具类专门校验用正则表达式
if (folderName == null || folderName.trim().isEmpty()) {
return Result.error("文件夹名称不能为空");
}
if (folderName.length() > 50) {
return Result.error("文件夹名称过长");
}
// 文件夹名称前置一个/
String folderName1 = "/" + folderName;
// 目标文件夹
File targetDir=Paths.get(businessDataPath, folderName1).toFile();
if(parentId != 0L){
// 获取父文件夹路径
targetDir = Paths.get(businessDataMapper.getPath(parentId), folderName1).toFile();
}
// 创建文件夹和新增文件夹路径
if (!targetDir.exists()) {
// 创建文件夹
boolean created = targetDir.mkdirs();
if (!created) {
throw new RuntimeException("文件夹创建失败: " + targetDir.getAbsolutePath());
}
//上面是新增文件夹功能但没有往数据库插入文件夹相关数据所以下面新增
// 创建BusinessDataEntity对象并设置属性
BusinessDataEntity businessDataEntity = new BusinessDataEntity(
null,
folderName,
parentId,
loginIdAsLong,
LocalDateTime.now(),
LocalDateTime.now(),
false,
targetDir.getAbsolutePath()
);
// 插入到数据库
businessDataMapper.insert(businessDataEntity);
return Result.okM( "文件夹创建成功");
} else {
return Result.error("文件夹已存在: ");
}
}
@ApiOperation("获取文件夹路径")
public String getPath(Long parentId) {
return businessDataMapper.getPath(parentId);
}
// @ApiOperation("删除文件夹")
// @Override
// public Result delete(Long folderId) {
// // 获取文件夹路径
// String folderPath = businessDataMapper.getPath(folderId);
//
// // 创建File对象并删除文件夹
// File folder = new File(folderPath);
// if (folder.exists()) {
// boolean deleted = folder.delete();
// if (!deleted) {
// // throw new RuntimeException("文件夹删除失败: " + folderPath);
// // TODO: 以后可以用全局异常处理器捕获或者用try catch捕获
// return Result.error("文件夹删除失败: " + folderPath);
// }
// //删除数据库中文件夹的数据
// businessDataMapper.delete(folderId);
// //删除文件夹下文件的数据
// businessDataFileService.delete(folderId);
// return Result.okM("删除成功");
// } else {
// // throw new RuntimeException("文件夹不存在: " + folderPath);
// return Result.error("文件夹不存在: " + folderPath);
// }
// }
@ApiOperation("删除文件夹")
@Override
public Result delete(Long folderId) {
// 获取文件夹路径
String folderPath = businessDataMapper.getPath(folderId);
// 创建Path对象并删除文件夹
Path folder = Paths.get(folderPath);
if (Files.exists(folder)) {
try {
// 使用Files.walk获取所有文件和目录按深度排序后删除
java.util.stream.Stream<Path> filePaths = Files.walk(folder);
filePaths.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
filePaths.close();
//删除数据库中文件夹的数据
businessDataMapper.delete(folderId);
//删除文件夹下文件的数据
businessDataFileService.delete(null , folderId);
return Result.okM("删除成功");
} catch (Exception e) {
return Result.okM("删除成功");
}
} else {
return Result.error("文件夹不存在: " + folderPath);
}
}
@ApiOperation("重命名文件夹")
@Override
public Result reName(Long folderId, String newName) {
// 获取文件夹路径
String folderPath = businessDataMapper.getPath(folderId);
String newPath = folderPath.substring(0, folderPath.lastIndexOf('\\'))+"\\" + newName;
System.out.printf("7777777"+newPath);
//
// //想命名的原文件的路径
// File file = new File("f:/a/a.xlsx");
// //将原文件更改为f:\a\b.xlsx其中路径是必要的注意
// file.renameTo(new File("f:/a/b.xlsx"));
//想命名的原文件夹的路径
File file1 = new File(folderPath);
//将原文件夹更改为A其中路径是必要的注意
file1.renameTo(new File(newPath));
LocalDateTime now = LocalDateTime.now();
BusinessDataEntity businessDataEntity = new BusinessDataEntity(
folderId,
newName,
null,
null,
null,
now,
null,
newPath
);
System.out.println(businessDataEntity);
businessDataMapper.reName(businessDataEntity);
return Result.okM("重命名成功");
}
}

View File

@ -88,9 +88,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
@Override
public List<UserListResp> list(UserListReq req) {
return this.baseMapper.queryBySelective(req).stream().map(resp -> {
resp.setUserTypeLabel(UserTypeEnum.getDescByCode(resp.getUserType()));
resp.setUserStatusLabel(UserStatusEnum.getDescByCode(resp.getUserStatus()));
return resp;
resp.setUserTypeLabel(UserTypeEnum.getDescByCode(resp.getUserType()));
resp.setUserStatusLabel(UserStatusEnum.getDescByCode(resp.getUserStatus()));
return resp;
}).collect(Collectors.toList());
}
@ -107,7 +107,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
UserEntity entity = this.getById(userId);
UserResp userResp= Converts.INSTANCE.toUserResp(entity);
if(StrUtil.isNotBlank(userResp.getDeptId())){
userResp.setUserId(deptService.getById(userResp.getDeptId()).getDeptName());
userResp.setDeptName(deptService.getById(userResp.getDeptId()).getDeptName());
}
userResp.setUserTypeLabel(UserTypeEnum.getDescByCode(userResp.getUserType()));
userResp.setUserStatusLabel(UserStatusEnum.getDescByCode(userResp.getUserStatus()));
@ -252,6 +252,4 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
entity.setDelFlag(Constants.DEL_FLAG_1);
this.updateById(entity);
}
}

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dite.znpt.mapper.BusinessDataFileMapper">
<select id="List" resultType="com.dite.znpt.domain.entity.BusinessDataFileEntity">
select * from business_data_part_file
<where>
<if test="param1 != null">
and folder_id = #{param1}
</if>
<if test="param2 != null and param2 != ''">
and file_name = #{param2}
</if>
</where>
</select>
<delete id="delete" parameterType="com.dite.znpt.domain.entity.BusinessDataFileEntity" >
delete from business_data_part_file
<where>
<if test="fileId != null">
and file_id = #{fileId}
</if>
<if test="folderId != null ">
and folder_id = #{folderId}
</if>
</where>
</delete>
<insert id="add" parameterType="com.dite.znpt.domain.entity.BusinessDataFileEntity">
insert into business_data_part_file (file_id, folder_id, file_name, file_path, file_type, file_size, upload_time, uploader_id, is_deleted)
values (#{fileId}, #{folderId}, #{fileName}, #{filePath}, #{fileType}, #{fileSize}, #{uploadTime}, #{uploaderId}, #{isDeleted})
</insert>
<select id="getPath" parameterType="java.lang.Long" resultType="java.lang.String">
select file_path from business_data_part_file
<where>
<if test="fileId != null">
and file_id = #{param1}
</if>
</where>
</select>
</mapper>

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dite.znpt.mapper.BusinessDataMapper">
<insert id="insert" parameterType="com.dite.znpt.domain.entity.BusinessDataEntity">
insert into business_data_part
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="folderName != null">folder_name,</if>
<if test="parentId != null">parent_id,</if>
<if test="creatorId != null">creator_id,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="isDeleted != null">is_deleted,</if>
<if test="folderPath != null">folder_path,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="folderName != null">#{folderName},</if>
<if test="parentId != null">#{parentId},</if>
<if test="creatorId != null">#{creatorId},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="isDeleted != null">#{isDeleted},</if>
<if test="folderPath != null">#{folderPath},</if>
</trim>
</insert>
<select id="List" resultType="com.dite.znpt.domain.entity.BusinessDataEntity">
select * from business_data_part
</select>
<select id="getPath" resultType="java.lang.String">
select folder_path from business_data_part where folder_id = #{parentId}
</select>
<delete id="delete">
delete from business_data_part where folder_id = #{folderId}
</delete>
<update id="reName">
update business_data_part
<set>
<if test="folderName != null">folder_name = #{folderName},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="folderPath != null">folder_path = #{folderPath},</if>
</set>
where folder_id = #{folderId}
</update>
</mapper>

View File

@ -0,0 +1,58 @@
package com.dite.znpt.web.controller;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.dto.FolderDto;
import com.dite.znpt.domain.page.PageBean;
import com.dite.znpt.service.BusinessDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/*
* @Description: 商务资料管理文件夹层
* 虽然文件夹本质也是文件但我们页面上很多地方光查文件信息根据文件夹ID
* 获取文件列表如果文件夹和文件都放一张表不好区分接口也不好区分这样分开写方便
*/
@Api(tags = "文件夹接口")
@RestController
@Slf4j
@RequestMapping("/businessData/folder")
@ApiOperation("商务资料管理文件夹contrller层")
public class BusinessDataController {
@Resource
private BusinessDataService businessDataService;
@ApiOperation(value = "分页查询文件夹", httpMethod = "GET")
@GetMapping("/list")
public Result pageSelect(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){
PageBean pageBean = businessDataService.pageSelect(page, pageSize);
return Result.ok(pageBean);
}
@ApiOperation(value = "增加文件夹", httpMethod = "POST")
@PostMapping("/creatFolder")
public Result createFolder(@RequestBody FolderDto folderDto) {
return businessDataService.createFolder(folderDto.getName(), folderDto.getParentId());
}
@ApiOperation(value = "删除文件夹", httpMethod = "DELETE")
@DeleteMapping("/delete")
public Result delete(@RequestParam Long folderId) {
return businessDataService.delete(folderId);
}
@ApiOperation(value = "重命名文件夹", httpMethod = "PUT")
@PutMapping("/rename")
public Result Rename(@RequestParam Long folderId, @RequestParam String newName) {
return businessDataService.reName(folderId, newName);
}
}

View File

@ -0,0 +1,137 @@
package com.dite.znpt.web.controller;
import com.dite.znpt.domain.Result;
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import com.dite.znpt.domain.page.PageBean;
import com.dite.znpt.mapper.BusinessDataFileMapper;
import com.dite.znpt.mapper.BusinessDataMapper;
import com.dite.znpt.service.BusinessDataFileService;
import com.dite.znpt.service.BusinessDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import org.springframework.web.multipart.MultipartFile;
/*
* @Description: 商务资料管理文件夹层
*/
@Api(tags = "文件接口")
@RestController
@Slf4j
@RequestMapping("/businessData/file")
@ApiOperation("商务资料管理,文件层")
public class BusinessDataFileController {
@Resource
private BusinessDataFileService businessDataFileService;
@Resource
private BusinessDataService businessDataService;
@Resource
private BusinessDataFileMapper businessDataFileMapper;
@ApiOperation(value = "分页查询文件", httpMethod = "GET")
@GetMapping("/list")
public Result pageSelect(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(defaultValue = "0") Long folderId) {
PageBean pageBean = businessDataFileService.pageSelect(page, pageSize, folderId);
return Result.ok(pageBean);
}
@ApiOperation(value = "增加文件")
@PostMapping("/add")
public Result add(@RequestParam("file") MultipartFile file,
@RequestParam Long folderId) {
System.out.println(file+" 77777777777777777777777777"+folderId);
if (file.isEmpty()) {
return Result.error("上传文件为空");
}
//TODO 以后可以优化就算文件名一样加个12这种
try {
byte[] bytes = file.getBytes();
String uploadDir = businessDataService.getPath(folderId);
File uploadedFile = new File(uploadDir + "\\" + file.getOriginalFilename());
if(uploadedFile.exists()){
return Result.error("文件已存在");
}
file.transferTo(uploadedFile);
//保存文件信息到数据
BusinessDataFileEntity fileEntity = new BusinessDataFileEntity();
fileEntity.setFolderId(folderId);
fileEntity.setFileName(file.getOriginalFilename());
fileEntity.setFilePath(uploadDir+"\\"+file.getOriginalFilename());
fileEntity.setFileType(file.getContentType());
fileEntity.setFileSize(file.getSize());
fileEntity.setUploadTime(new Date() );
fileEntity.setUploaderId(0L);
System.out.println(uploadDir+"\\"+file.getOriginalFilename());
businessDataFileService.add(fileEntity);
return Result.okM("上传成功");
} catch (IOException e) {
e.printStackTrace();
return Result.error("上传失败");
}
}
@ApiOperation(value = "删除文件")
@DeleteMapping("/delete")
public Result delete(@RequestParam Long fileId) {
businessDataFileService.delete(fileId,null);
return Result.okM("删除成功");
}
@ApiOperation(value = "下载文件")
@GetMapping("/download")
public void download(@RequestParam("fileId") Long fileId , HttpServletResponse response) {
String path = businessDataFileService.getPath(fileId);
try {
// path是指想要下载的文件的路径
File file = new File(path);
log.info(file.getPath());
// 获取文件名
String filename = file.getName();
// 获取文件后缀名
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
log.info("文件后缀名:" + ext);
// 将文件写入输入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知浏览器文件的大小
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

View File

@ -2,6 +2,7 @@
server:
# 服务器的HTTP端口默认为8080
port: 8888
address: 0.0.0.0
# 数据源配置
spring:
@ -148,3 +149,7 @@ util:
imagePreTreatmentPath: d:\
reportGeneratorPath: D:\blade_report_generator.exe
reportGeneratorTemplatePath: D:\muban
file:
upload:
businessDataPath: D:/upload/businessData