Ai-interviewer-system/src/main/java/com/yzdx/AiInterviewer/controller/UploadController.java

264 lines
8.9 KiB
Java
Raw Normal View History

2023-11-04 16:30:50 +00:00
package com.yzdx.AiInterviewer.controller;
2023-11-10 10:28:31 +00:00
2023-11-04 16:30:50 +00:00
import com.yzdx.AiInterviewer.comment.R;
2023-11-10 10:28:31 +00:00
import com.yzdx.AiInterviewer.entity.CompanyImage;
import com.yzdx.AiInterviewer.mapper.CompanyImageMapper;
import com.yzdx.AiInterviewer.utiles.ParseResumeUtil;
2023-11-04 16:30:50 +00:00
import lombok.extern.slf4j.Slf4j;
2023-11-10 10:28:31 +00:00
import org.springframework.beans.factory.annotation.Autowired;
2023-11-04 16:30:50 +00:00
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;
@RestController
@Slf4j
public class UploadController {
2023-11-10 10:28:31 +00:00
@Autowired
private CompanyImageMapper companyImageMapper;
2023-11-04 16:30:50 +00:00
//设置上传文件的最大大小
public static final int MAX_SIZE=10*1024*1024;
2023-11-10 10:28:31 +00:00
public static final String UPLOAD_AVATAR_PATH = "D:\\vueStudy\\Ai-interviewer-ui\\src\\upload\\picture";//获取上传文件的路径获取项目中名为upload的文件夹
public static final String UPLOAD_RESUME_PATH = "D:\\vueStudy\\Ai-interviewer-ui\\src\\upload\\resume";//获取上传文件的路径获取项目中名为upload的文件夹
2023-11-04 16:30:50 +00:00
//设置文件的类型
public static final List<String> AVATAR_TYPE=new ArrayList<>();
static {
AVATAR_TYPE.add("image/png");
AVATAR_TYPE.add("image/gif");
AVATAR_TYPE.add("image/bmp");
AVATAR_TYPE.add("image/jpg");
AVATAR_TYPE.add("image/jpeg");
}
2023-11-10 10:28:31 +00:00
@PostMapping("/upload_companyImage")
public R upLoadPicture(HttpSession session, @RequestParam("file") MultipartFile file ,String encoding){
2023-11-04 16:30:50 +00:00
//MultipartFile是spring提供的类可以接收所有的文件的类
log.info(file.getContentType());
if(file.isEmpty()){
return R.error("请选择文件");
}
if (file.getSize() >MAX_SIZE){//file.getSize()获取接收文件的大小
return R.error("文件大小超出最大限制");
}
if(!AVATAR_TYPE.contains(file.getContentType())){//自定义接收文件的类型
return R.error("文件类型不匹配");
}
// String uploadPath = session.getServletContext().getRealPath("upload");//获取上传文件的路径获取项目中名为upload的文件夹
2023-11-10 10:28:31 +00:00
// String uploadPath = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径获取项目中名为upload的文件夹
//
// log.info(uploadPath);
File dir=new File(UPLOAD_AVATAR_PATH);//指向名为upload文件夹
if(!dir.exists()){
dir.mkdirs();//若不存在,则创建该文件夹
}
String originalFilename = file.getOriginalFilename();//获取文件的真实文件名
int index = originalFilename.lastIndexOf(".");//获取文件的后缀名‘.’的位置
String substring = originalFilename.substring(index);//返回文件类型名 例如:.jpg
2023-11-04 16:30:50 +00:00
2023-11-10 10:28:31 +00:00
String filename = UUID.randomUUID().toString().toUpperCase()+substring;//新创建的文件名
File dest=new File(dir,filename);//创建一个空的文件
try {
file.transferTo(dest);
} catch (IOException e) {
return R.error("文件存储出现异常");
}
String RealFilePath="http://localhost:5173/src/upload/picture/"+filename;
CompanyImage companyImage=new CompanyImage();
companyImage.setImage(RealFilePath);
companyImage.setFilename(filename);
companyImage.setEncoding(encoding);
companyImageMapper.insert(companyImage);
return R.success(companyImage);//返回图片存储在服务器的地址
}
@GetMapping("/delete_picture")
public R deletePicture(String imagePath){
if(imagePath==null||imagePath.equals("")){
return R.error("删除失败!");
}
try {
File folder = new File(UPLOAD_AVATAR_PATH);
File[] files = folder.listFiles();
for(File file:files){
if(file.getName().equals(imagePath)){
file.delete();
}
}
return R.success("删除成功");
} catch (Exception e) {
return R.error("删除失败");
}
}
@PostMapping("/upload_resume")
public R uploadResume(HttpSession session, @RequestParam("file") MultipartFile file){
if(file.isEmpty()){
return R.error("请选择文件");
}
if (file.getSize() >50*1024*1024){//file.getSize()获取接收文件的大小
return R.error("文件大小超出最大限制");
}
// String uploadPath = session.getServletContext().getRealPath("upload");//获取上传文件的路径获取项目中名为upload的文件夹
// String uploadPath = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径获取项目中名为upload的文件夹
//
// log.info(uploadPath);
2023-11-04 16:30:50 +00:00
2023-11-10 10:28:31 +00:00
File dir=new File(UPLOAD_RESUME_PATH);//指向名为upload文件夹
2023-11-04 16:30:50 +00:00
if(!dir.exists()){
dir.mkdirs();//若不存在,则创建该文件夹
}
String originalFilename = file.getOriginalFilename();//获取文件的真实文件名
int index = originalFilename.lastIndexOf(".");//获取文件的后缀名‘.’的位置
String substring = originalFilename.substring(index);//返回文件类型名 例如:.jpg
String filename = UUID.randomUUID().toString().toUpperCase()+substring;//新创建的文件名
File dest=new File(dir,filename);//创建一个空的文件
try {
file.transferTo(dest);
} catch (IOException e) {
return R.error("文件存储出现异常");
}
2023-11-10 10:28:31 +00:00
String RealFilePath="http://localhost:5173/src/upload/resume/"+filename;
ParseResumeUtil.ParseChineseResume(UPLOAD_RESUME_PATH+"\\"+filename);
2023-11-04 16:30:50 +00:00
Map<String,Object> data=new HashMap<>();
2023-11-10 10:28:31 +00:00
data.put("resume",RealFilePath);
2023-11-04 16:30:50 +00:00
data.put("filename",filename);
return R.success(data);//返回图片存储在服务器的地址
2023-11-10 10:28:31 +00:00
2023-11-04 16:30:50 +00:00
}
2023-11-10 10:28:31 +00:00
@GetMapping("/delete_resume")
public R deleteResume(String resumePath){
if(resumePath==null||resumePath.equals("")){
2023-11-04 16:30:50 +00:00
return R.error("删除失败!");
}
try {
2023-11-10 10:28:31 +00:00
File folder = new File(UPLOAD_AVATAR_PATH);
2023-11-04 16:30:50 +00:00
File[] files = folder.listFiles();
for(File file:files){
2023-11-10 10:28:31 +00:00
if(file.getName().equals(resumePath)){
2023-11-04 16:30:50 +00:00
file.delete();
}
}
return R.success("删除成功");
} catch (Exception e) {
return R.error("删除失败");
}
}
2023-11-10 10:28:31 +00:00
@PostMapping("/upload_picture")
public R uploadCompanyImage(HttpSession session, @RequestParam("file") MultipartFile file){
//MultipartFile是spring提供的类可以接收所有的文件的类
log.info(file.getContentType());
if(file.isEmpty()){
return R.error("请选择文件");
}
if (file.getSize() >MAX_SIZE){//file.getSize()获取接收文件的大小
return R.error("文件大小超出最大限制");
}
if(!AVATAR_TYPE.contains(file.getContentType())){//自定义接收文件的类型
return R.error("文件类型不匹配");
}
// String uploadPath = session.getServletContext().getRealPath("upload");//获取上传文件的路径获取项目中名为upload的文件夹
// String uploadPath = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径获取项目中名为upload的文件夹
//
// log.info(uploadPath);
File dir=new File(UPLOAD_AVATAR_PATH);//指向名为upload文件夹
if(!dir.exists()){
dir.mkdirs();//若不存在,则创建该文件夹
}
String originalFilename = file.getOriginalFilename();//获取文件的真实文件名
int index = originalFilename.lastIndexOf(".");//获取文件的后缀名‘.’的位置
String substring = originalFilename.substring(index);//返回文件类型名 例如:.jpg
String filename= UUID.randomUUID().toString().toUpperCase()+substring;//新创建的文件名
File dest=new File(dir,filename);//创建一个空的文件
try {
file.transferTo(dest);
} catch (IOException e) {
return R.error("文件存储出现异常");
}
String RealFilePath="http://localhost:5173/src/upload/picture/"+filename;
Map<String,Object> data=new HashMap<>();
data.put("image",RealFilePath);
data.put("filename",filename);
return R.success(data);//返回图片存储在服务器的地址
}
2023-11-04 16:30:50 +00:00
}
2023-11-10 10:28:31 +00:00