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

314 lines
9.2 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.utiles.ParseResumeUtil;
2023-11-20 02:38:07 +00:00
import io.swagger.annotations.Api;
2023-11-04 16:30:50 +00:00
import lombok.extern.slf4j.Slf4j;
2023-11-20 02:38:07 +00:00
import org.springframework.web.bind.annotation.*;
2023-11-04 16:30:50 +00:00
import org.springframework.web.multipart.MultipartFile;
2023-11-16 13:22:51 +00:00
import javax.servlet.http.HttpServletRequest;
2023-11-04 16:30:50 +00:00
import java.io.File;
import java.io.IOException;
import java.util.*;
2023-11-12 06:00:40 +00:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2023-11-04 16:30:50 +00:00
2023-11-20 02:38:07 +00:00
2023-11-04 16:30:50 +00:00
@RestController
@Slf4j
public class UploadController {
//设置上传文件的最大大小
public static final int MAX_SIZE=10*1024*1024;
//设置文件的类型
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-18 07:47:31 +00:00
/**
* 上传图片
* @param request
* @param file
* @return R
*/
2023-11-16 13:22:51 +00:00
@PostMapping("/upload_picture")
public R upLoadPicture(HttpServletRequest request, @RequestParam("file") MultipartFile file){
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("文件类型不匹配");
}
2023-11-16 13:22:51 +00:00
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/picture/");//获取上传文件的路径获取项目中名为upload的文件夹
2023-11-10 10:28:31 +00:00
// String uploadPath = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径获取项目中名为upload的文件夹
//
2023-11-16 13:22:51 +00:00
log.info(uploadPath);
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
//指向名为upload文件夹
File dir= new File(uploadPath);
2023-11-10 10:28:31 +00:00
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("文件存储出现异常");
}
2023-11-16 13:22:51 +00:00
String RealFilePath="http://localhost:8080/upload/picture/"+filename;
2023-11-12 06:00:40 +00:00
Map<String,Object> data=new HashMap<>();
data.put("image",RealFilePath);
data.put("filename",filename);
return R.success(data);//返回图片存储在服务器的地址
2023-11-10 10:28:31 +00:00
}
2023-11-18 07:47:31 +00:00
/**
* 上传视频
* @param request
* @param file
* @return R
*/
2023-11-16 13:22:51 +00:00
@PostMapping("/upload_video")
public R upLoadVideo(HttpServletRequest request, @RequestParam("file") MultipartFile file ){
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
//MultipartFile是spring提供的类可以接收所有的文件的类
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
log.info(file.getContentType());
2023-11-10 10:28:31 +00:00
if(file.isEmpty()){
return R.error("请选择文件");
}
2023-11-16 13:22:51 +00:00
if (file.getSize() >MAX_SIZE){//file.getSize()获取接收文件的大小
2023-11-10 10:28:31 +00:00
return R.error("文件大小超出最大限制");
}
2023-11-04 16:30:50 +00:00
2023-11-16 13:22:51 +00:00
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/video/");//获取上传文件的路径获取项目中名为upload的文件夹
File dir= new File(uploadPath);//指向名为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
2023-11-16 13:22:51 +00:00
String filename = UUID.randomUUID().toString().toUpperCase()+substring;//新创建的文件名
2023-11-04 16:30:50 +00:00
2023-11-16 13:22:51 +00:00
File dest=new File(dir,filename);//创建一个空的文件
2023-11-04 16:30:50 +00:00
try {
file.transferTo(dest);
} catch (IOException e) {
return R.error("文件存储出现异常");
}
2023-11-16 13:22:51 +00:00
String RealFilePath="http://localhost:8080/upload/video/"+filename;
2023-11-04 16:30:50 +00:00
Map<String,Object> data=new HashMap<>();
2023-11-16 13:22:51 +00:00
data.put("video",RealFilePath);
data.put("filename",filename);
2023-11-04 16:30:50 +00:00
return R.success(data);//返回图片存储在服务器的地址
}
2023-11-18 07:47:31 +00:00
/**
* 删除照片
* @param request
* @param imagePath
* @return R
*/
2023-11-20 02:38:07 +00:00
@DeleteMapping("/delete_picture")
2023-11-16 13:22:51 +00:00
public R deletePicture(HttpServletRequest request,String imagePath){
if(imagePath==null||imagePath.equals("")){
2023-11-04 16:30:50 +00:00
return R.error("删除失败!");
}
2023-11-16 13:22:51 +00:00
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/picture/");
2023-11-04 16:30:50 +00:00
try {
2023-11-16 13:22:51 +00:00
File folder = new File(uploadPath);
2023-11-04 16:30:50 +00:00
File[] files = folder.listFiles();
for(File file:files){
2023-11-16 13:22:51 +00:00
if(file.getName().equals(imagePath)){
2023-11-04 16:30:50 +00:00
file.delete();
}
}
return R.success("删除成功");
} catch (Exception e) {
return R.error("删除失败");
}
}
2023-11-18 07:47:31 +00:00
/**
* 删除视频
* @param request
* @param videoPath
* @return R
*/
2023-11-20 02:38:07 +00:00
@DeleteMapping("/delete_video")
2023-11-16 13:22:51 +00:00
public R deleteVideo(HttpServletRequest request,String videoPath){
if(videoPath==null||videoPath.equals("")){
return R.error("删除失败!");
}
2023-11-19 05:19:41 +00:00
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/video/");
2023-11-16 13:22:51 +00:00
try {
File folder = new File(uploadPath);
File[] files = folder.listFiles();
for(File file:files){
if(file.getName().equals(videoPath)){
file.delete();
}
}
return R.success("删除成功");
} catch (Exception e) {
return R.error("删除失败");
}
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
}
2023-11-10 10:28:31 +00:00
2023-11-18 07:47:31 +00:00
/**
* 上传简历
* @param request
* @param file
* @return R
*/
2023-11-16 13:22:51 +00:00
@PostMapping("/upload_resume")
public R uploadResume(HttpServletRequest request, @RequestParam("file") MultipartFile file){
2023-11-10 10:28:31 +00:00
if(file.isEmpty()){
return R.error("请选择文件");
}
2023-11-16 13:22:51 +00:00
if (file.getSize() >50*1024*1024){//file.getSize()获取接收文件的大小
2023-11-10 10:28:31 +00:00
return R.error("文件大小超出最大限制");
}
2023-11-16 13:22:51 +00:00
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/resume/");
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
File dir= new File(uploadPath);
2023-11-10 10:28:31 +00:00
if(!dir.exists()){
dir.mkdirs();//若不存在,则创建该文件夹
}
String originalFilename = file.getOriginalFilename();//获取文件的真实文件名
int index = originalFilename.lastIndexOf(".");//获取文件的后缀名‘.’的位置
String substring = originalFilename.substring(index);//返回文件类型名 例如:.jpg
2023-11-16 13:22:51 +00:00
String fileName = UUID.randomUUID().toString().toUpperCase()+substring;//新创建的文件名
2023-11-10 10:28:31 +00:00
2023-11-16 13:22:51 +00:00
File dest=new File(dir,fileName);//创建一个空的文件
2023-11-10 10:28:31 +00:00
try {
file.transferTo(dest);
} catch (IOException e) {
return R.error("文件存储出现异常");
}
2023-11-16 13:22:51 +00:00
String RealFilePath="http://localhost:8080/upload/resume/"+fileName;
ExecutorService pool= Executors.newCachedThreadPool();
pool.submit(new Runnable() {
@Override
public void run() {
ParseResumeUtil.ParseChineseResume( request.getSession().getServletContext().getRealPath("/upload/resume/")+"\\"+fileName);
}
});
2023-11-10 10:28:31 +00:00
Map<String,Object> data=new HashMap<>();
2023-11-16 13:22:51 +00:00
data.put("resume",RealFilePath);
data.put("filename",fileName);
2023-11-10 10:28:31 +00:00
return R.success(data);//返回图片存储在服务器的地址
2023-11-16 13:22:51 +00:00
2023-11-10 10:28:31 +00:00
}
2023-11-18 07:47:31 +00:00
/**
* 删除简历
* @param request
* @param resumePath
* @return R
*/
2023-11-20 02:38:07 +00:00
@DeleteMapping("/delete_resume")
2023-11-16 13:22:51 +00:00
public R deleteResume(HttpServletRequest request,String resumePath){
if(resumePath==null||resumePath.equals("")){
return R.error("删除失败!");
}
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/resume/");
try {
File folder = new File(uploadPath);
File[] files = folder.listFiles();
for(File file:files){
if(file.getName().equals(resumePath)){
file.delete();
}
}
return R.success("删除成功");
} catch (Exception e) {
return R.error("删除失败");
}
}
2023-11-10 10:28:31 +00:00
2023-11-04 16:30:50 +00:00
}
2023-11-10 10:28:31 +00:00