上传密码本功能开发
This commit is contained in:
parent
b55405fc6b
commit
b92fb37a3f
|
|
@ -0,0 +1,14 @@
|
|||
package com.bonus.gs.sub.evaluate.evaluate.beans;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author:liang.chao
|
||||
* @Date:2025/5/23 - 15:23
|
||||
*/
|
||||
@Data
|
||||
public class FileInfoBean {
|
||||
private String id;
|
||||
private String fileName;
|
||||
private String filePath;
|
||||
}
|
||||
|
|
@ -1,16 +1,17 @@
|
|||
package com.bonus.gs.sub.evaluate.evaluate.controller;
|
||||
|
||||
import com.bonus.gs.sub.evaluate.evaluate.beans.FileInfoBean;
|
||||
import com.bonus.gs.sub.evaluate.evaluate.service.FileInfoService;
|
||||
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:liang.chao
|
||||
|
|
@ -21,12 +22,15 @@ import java.io.File;
|
|||
@RequestMapping("/upload")
|
||||
@Slf4j
|
||||
public class UploadController {
|
||||
@Autowired
|
||||
private FileInfoService fileInfoService;
|
||||
|
||||
@Value("${upload.dir}")
|
||||
private String uploadDir;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -70,4 +74,71 @@ public class UploadController {
|
|||
}
|
||||
return ar;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/passWordFile", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public AjaxRes uploadPassWordFile(@RequestParam("file") MultipartFile file) {
|
||||
AjaxRes ar = new AjaxRes();
|
||||
if (file != null && !file.isEmpty()) {
|
||||
// 验证文件类型
|
||||
String originalFileName = file.getOriginalFilename();
|
||||
String fileExtension = originalFileName != null && originalFileName.contains(".")
|
||||
? originalFileName.split("\\.")[originalFileName.split("\\.").length - 1] : "";
|
||||
if (!"xls".equalsIgnoreCase(fileExtension)
|
||||
&& !"xlsx".equalsIgnoreCase(fileExtension)
|
||||
&& !"doc".equalsIgnoreCase(fileExtension)
|
||||
&& !"docx".equalsIgnoreCase(fileExtension)
|
||||
&& !"pdf".equalsIgnoreCase(fileExtension)
|
||||
&& !"jpg".equalsIgnoreCase(fileExtension)
|
||||
&& !"jpeg".equalsIgnoreCase(fileExtension)
|
||||
&& !"png".equalsIgnoreCase(fileExtension)) {
|
||||
ar.setFailMsg("文件类型错误,请上传xls/xlsx/doc/docx/jpg/jpeg/png/pdf格式的文件");
|
||||
return ar;
|
||||
}
|
||||
}
|
||||
try {
|
||||
File targetDir = new File(uploadDir);
|
||||
if (!targetDir.exists()) {
|
||||
targetDir.mkdirs();
|
||||
}
|
||||
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
|
||||
File targetFile = new File(uploadDir, fileName);
|
||||
file.transferTo(targetFile);
|
||||
// 返回文件路径
|
||||
String pathname = targetFile.getAbsolutePath();
|
||||
ar.setSucceedMsg("文件上传成功");
|
||||
ar.setSucceed(pathname);
|
||||
ar.setMessage(file.getOriginalFilename());
|
||||
FileInfoBean fileInfoBean = new FileInfoBean();
|
||||
fileInfoBean.setFileName(file.getOriginalFilename());
|
||||
fileInfoBean.setFilePath(pathname);
|
||||
fileInfoService.saveFile(fileInfoBean);
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(), e);
|
||||
ar.setFailMsg("文件上传失败");
|
||||
}
|
||||
return ar;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/getFile", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public AjaxRes getFile(FileInfoBean info) {
|
||||
AjaxRes ar = new AjaxRes();
|
||||
List<FileInfoBean> fileList = fileInfoService.getFile(info);
|
||||
ar.setSucceed(fileList);
|
||||
return ar;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/delFile", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public AjaxRes delFile(FileInfoBean info) {
|
||||
AjaxRes ar = new AjaxRes();
|
||||
Integer i = fileInfoService.delFile(info);
|
||||
if (i > 0) {
|
||||
ar.setSucceed("删除成功");
|
||||
} else {
|
||||
ar.setFailMsg("删除失败");
|
||||
}
|
||||
return ar;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package com.bonus.gs.sub.evaluate.evaluate.dao;
|
||||
|
||||
import com.bonus.gs.sub.evaluate.evaluate.beans.FileInfoBean;
|
||||
import com.bonus.gs.sub.evaluate.manager.entity.ResourcesBean;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:liang.chao
|
||||
* @Date:2025/5/23 - 15:27
|
||||
*/
|
||||
@Mapper
|
||||
public interface FileInfoDao {
|
||||
List<FileInfoBean> getFile(FileInfoBean info);
|
||||
|
||||
Integer delFile(FileInfoBean info);
|
||||
|
||||
Integer saveFile(FileInfoBean fileInfo);
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.bonus.gs.sub.evaluate.evaluate.service;
|
||||
|
||||
import com.bonus.gs.sub.evaluate.evaluate.beans.FileInfoBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:liang.chao
|
||||
* @Date:2025/5/23 - 15:23
|
||||
*/
|
||||
public interface FileInfoService {
|
||||
List<FileInfoBean> getFile(FileInfoBean info);
|
||||
|
||||
Integer delFile(FileInfoBean info);
|
||||
|
||||
Integer saveFile(FileInfoBean fileInfo);
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.bonus.gs.sub.evaluate.evaluate.service;
|
||||
|
||||
import com.bonus.gs.sub.evaluate.evaluate.beans.FileInfoBean;
|
||||
import com.bonus.gs.sub.evaluate.evaluate.dao.FileInfoDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:liang.chao
|
||||
* @Date:2025/5/23 - 15:23
|
||||
*/
|
||||
@Service
|
||||
public class FileInfoServiceImpl implements FileInfoService{
|
||||
|
||||
@Autowired
|
||||
private FileInfoDao fileInfoDao;
|
||||
@Override
|
||||
public List<FileInfoBean> getFile(FileInfoBean info) {
|
||||
return fileInfoDao.getFile(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer delFile(FileInfoBean info) {
|
||||
return fileInfoDao.delFile(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer saveFile(FileInfoBean fileInfo) {
|
||||
return fileInfoDao.saveFile(fileInfo);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?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.bonus.gs.sub.evaluate.evaluate.dao.FileInfoDao">
|
||||
<insert id="saveFile">
|
||||
insert into file_info(file_name, file_path)
|
||||
values (#{fileName}, #{filePath})
|
||||
</insert>
|
||||
<delete id="delFile">
|
||||
delete
|
||||
from file_info
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<select id="getFile" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.FileInfoBean">
|
||||
select id, file_name as fileName, file_path as filePath
|
||||
from file_info
|
||||
<if test="id != null">
|
||||
where id = #{id}
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -142,12 +142,12 @@
|
|||
}
|
||||
if (res.obj && res.obj.length > 0) {
|
||||
$('.password-book-list').empty()
|
||||
res.obj.forEach(item => {
|
||||
res.obj.forEach((item) => {
|
||||
$('.password-book-list').append(
|
||||
'<div class="password-book-item">' +
|
||||
'<span>' + item.fileName + '</span>' +
|
||||
`<span onclick="deleteFile('${item.id}')">删除</span>` + // 直接传 id
|
||||
`<span onclick="downloadFile('${item.filePtah}')">预览</span>` + // 直接传 id
|
||||
`<span onclick="downloadFile('${item.id}')">预览</span>` + // 直接传 id
|
||||
'</div>'
|
||||
);
|
||||
})
|
||||
|
|
@ -172,7 +172,8 @@
|
|||
})
|
||||
}
|
||||
// 预览文件
|
||||
function downloadFile(filePath) {
|
||||
function downloadFile(id) {
|
||||
const filePath = fileList.filter(item => item.id === id)[0].filePath
|
||||
window.open(filePath)
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue