326 lines
9.9 KiB
Java
326 lines
9.9 KiB
Java
package com.yzdx.AiInterviewer.controller;
|
||
|
||
|
||
import com.yzdx.AiInterviewer.comment.R;
|
||
import com.yzdx.AiInterviewer.utiles.ParseResumeUtil;
|
||
|
||
import io.swagger.annotations.ApiImplicitParam;
|
||
import io.swagger.annotations.ApiImplicitParams;
|
||
import io.swagger.annotations.ApiOperation;
|
||
import io.swagger.annotations.ApiParam;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.*;
|
||
import java.util.concurrent.ExecutorService;
|
||
import java.util.concurrent.Executors;
|
||
|
||
|
||
@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");
|
||
}
|
||
|
||
/**
|
||
* 上传图片
|
||
* @param request
|
||
*/
|
||
@ResponseBody
|
||
@ApiImplicitParams({
|
||
@ApiImplicitParam(name = "file",required = true),
|
||
})
|
||
@PostMapping("/upload_picture")
|
||
@ApiOperation(value = "上传图片",notes = "")
|
||
|
||
public R upLoadPicture(HttpServletRequest request, @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 = request.getSession().getServletContext().getRealPath("/upload/picture/");//获取上传文件的路径(获取项目中名为‘upload’的文件夹)
|
||
// String uploadPath = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径(获取项目中名为‘upload’的文件夹)
|
||
//
|
||
log.info(uploadPath);
|
||
|
||
//指向名为‘upload’文件夹
|
||
|
||
File dir= new File(uploadPath);
|
||
|
||
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://117.88.94.226:8080/upload/picture/"+filename;
|
||
Map<String,Object> data=new HashMap<>();
|
||
data.put("image",RealFilePath);
|
||
data.put("filename",filename);
|
||
return R.success(data);//返回图片存储在服务器的地址
|
||
}
|
||
|
||
/**
|
||
* 上传视频
|
||
* @param request
|
||
* @param file
|
||
* @return R
|
||
*/
|
||
@PostMapping("/upload_video")
|
||
@ApiOperation(value = "上传视频",notes = "")
|
||
|
||
public R upLoadVideo(HttpServletRequest request, @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("文件大小超出最大限制");
|
||
}
|
||
|
||
|
||
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/video/");//获取上传文件的路径(获取项目中名为‘upload’的文件夹)
|
||
|
||
|
||
File dir= new File(uploadPath);//指向名为‘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://117.88.94.226:8080/upload/video/"+filename;
|
||
Map<String,Object> data=new HashMap<>();
|
||
data.put("video",RealFilePath);
|
||
data.put("filename",filename);
|
||
return R.success(data);//返回图片存储在服务器的地址
|
||
}
|
||
|
||
/**
|
||
* 删除照片
|
||
* @param request
|
||
* @param imagePath
|
||
* @return R
|
||
*/
|
||
@DeleteMapping("/delete_picture")
|
||
@ApiOperation(value = "删除图片",notes = "")
|
||
|
||
public R deletePicture(@ApiParam("String imagePath") HttpServletRequest request, String imagePath){
|
||
if(imagePath==null||imagePath.equals("")){
|
||
return R.error("删除失败!");
|
||
}
|
||
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/picture/");
|
||
try {
|
||
File folder = new File(uploadPath);
|
||
File[] files = folder.listFiles();
|
||
for(File file:files){
|
||
if(file.getName().equals(imagePath)){
|
||
file.delete();
|
||
}
|
||
}
|
||
return R.success("删除成功");
|
||
} catch (Exception e) {
|
||
return R.error("删除失败");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 删除视频
|
||
* @param request
|
||
* @param videoPath
|
||
* @return R
|
||
*/
|
||
@DeleteMapping("/delete_video")
|
||
@ApiOperation(value = "删除图片",notes = "")
|
||
|
||
public R deleteVideo(@ApiParam("String videoPath") HttpServletRequest request,String videoPath){
|
||
if(videoPath==null||videoPath.equals("")){
|
||
return R.error("删除失败!");
|
||
}
|
||
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/video/");
|
||
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("删除失败");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 上传简历
|
||
* @param request
|
||
* @param file
|
||
* @return R
|
||
*/
|
||
@PostMapping("/upload_resume")
|
||
@ApiOperation(value = "上传简历",notes = "")
|
||
|
||
public R uploadResume(HttpServletRequest request, @RequestParam("file") MultipartFile file){
|
||
if(file.isEmpty()){
|
||
return R.error("请选择文件");
|
||
}
|
||
|
||
if (file.getSize() >50*1024*1024){//file.getSize()获取接收文件的大小
|
||
return R.error("文件大小超出最大限制");
|
||
}
|
||
|
||
String uploadPath = request.getSession().getServletContext().getRealPath("/upload/resume/");
|
||
|
||
File dir= new File(uploadPath);
|
||
|
||
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://117.88.94.226: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);
|
||
}
|
||
});
|
||
|
||
|
||
Map<String,Object> data=new HashMap<>();
|
||
data.put("resume",RealFilePath);
|
||
data.put("filename",fileName);
|
||
|
||
return R.success(data);//返回图片存储在服务器的地址
|
||
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 删除简历
|
||
* @param request
|
||
* @param resumePath
|
||
* @return R
|
||
*/
|
||
@DeleteMapping("/delete_resume")
|
||
@ApiOperation(value = "删除简历",notes = "")
|
||
|
||
public R deleteResume(@ApiParam("String resumePath") 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("删除失败");
|
||
}
|
||
}
|
||
}
|
||
|
||
|