116 lines
3.7 KiB
Java
116 lines
3.7 KiB
Java
|
package com.yzdx.AiInterviewer.controller;
|
|||
|
|
|||
|
import com.yzdx.AiInterviewer.comment.R;
|
|||
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
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 {
|
|||
|
|
|||
|
//设置上传文件的最大大小
|
|||
|
public static final int MAX_SIZE=10*1024*1024;
|
|||
|
public static final String UPLOAD_PATH = "D:\\vue学习\\Ai-interviewer-ui\\src\\upload";//获取上传文件的路径(获取项目中名为‘upload’的文件夹)
|
|||
|
//设置文件的类型
|
|||
|
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");
|
|||
|
}
|
|||
|
|
|||
|
@PostMapping("/upload_picture")
|
|||
|
public R upLoadPicture(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_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/"+filename;
|
|||
|
|
|||
|
Map<String,Object> data=new HashMap<>();
|
|||
|
data.put("image",RealFilePath);
|
|||
|
data.put("filename",filename);
|
|||
|
|
|||
|
return R.success(data);//返回图片存储在服务器的地址
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@GetMapping("/delete_picture")
|
|||
|
public R deletePicture(String imagePath){
|
|||
|
if(imagePath==null||imagePath.equals("")){
|
|||
|
return R.error("删除失败!");
|
|||
|
}
|
|||
|
|
|||
|
try {
|
|||
|
File folder = new File(UPLOAD_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("删除失败");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|