1.conterller层
package com.jt.controller;
import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
@Autowired
private FileService fileService;
/**
* 需求分析: 文件上传完成之后,需要返回ImageVO对象
* @param file
* @return
* @throws IOException
*/
@PostMapping("/upload")
public SysResult upload(MultipartFile file) throws IOException {
ImageVO imageVO = fileService.upload(file);
//不成功 应该返回null
if(imageVO == null){
return SysResult.fail();
}
return SysResult.success(imageVO);
}
/**
* 需求: 实现图片删除
* URL: http://localhost:8091/file/deleteFile?virtualPath=xxx
* 参数: virtualPath= /2021/11/11/xxx.jpg
* 返回值结果: SysResult
*/
@DeleteMapping("/deleteFile")
public SysResult deleteFile(String virtualPath){
fileService.deleteFile(virtualPath);
return SysResult.success();
}
/**
* 业务: 文件上传入门案例
* URL: http://localhost:8091/file/upload
* 参数: file=[101001010111]
* 返回值: SysResult对象(ImageVO)
* 知识回顾: 字节流/字符流/缓存流 默认的语法复杂
* 高级API: SpringMVC 专门针对与流,开发了一个高级API
* 文件上传步骤:
* 1.获取文件上传名称
* 2.准备文件上传的目录
* 3.准备全文件的路径 目录/文件名称
* 4.实现上传
*/
/* @PostMapping("/upload")
public SysResult upload(MultipartFile file) throws IOException {
//1.动态获取文件名称
String fileName = file.getOriginalFilename();
//2.准备文件目录 Linux系统不能识别\
String dirPath = "F:/images";
File dirFile = new File(dirPath);
if(!dirFile.exists()){
//应该创建一个新目录 创建多级目录
dirFile.mkdirs();
}
//3.拼接文件路径
String filePath = "F:/images/" + fileName;
//4.实现文件上传
file.transferTo(new File(filePath));
System.out.println("实现文件上传");
return SysResult.success();
}*/
}
2.service层
package com.jt.service;
import com.jt.vo.ImageVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Service
public class FileServiceImpl implements FileService{
/**
* 规则说明:
* 文件磁盘地址: F:/images/yyyy/MM/dd/uuid.jpg
* 网络访问地址: http://image.jt.com/yyyy/MM/dd/uuid.jpg
*/
//private String localDir = "D:/images"; //本地磁盘前缀
private String localDir = "/usr/local/src/images"; //设定Linux目录
private String preURLPath = "http://image.jt.com"; //网络访问域名
/**
* 1.校验文件上传的类型 jpg|png|gif
* 2.应该校验文件是否为恶意程序. 木马.exe.jpg
* 3.为了提高检索效率 应该分目录存储. 1.hash方式 xx/xx/xx/xx 分布不均
* 2.日期格式 yyyy/MM/dd 目录不断增长
* 4.防止文件重名 UUID.jpg
* @param file
* @return
*/
@Override
public ImageVO upload(MultipartFile file) {
//1.图片类型的校验 正则表达式 aaa.jpg
String fileName = file.getOriginalFilename();
//字符大小写 干扰正则的判断 将所有的文件转化为小写字母
fileName = fileName.toLowerCase();
//程序不满足正则, 则用户上传的图片有问题
if(!fileName.matches("^.+\\.(jpg|png|gif)$")){
return null;
}
//2. 校验文件是否为恶意程序 判断依据 属性宽度和高度 aa.exe.jpg
try {
//该对象是用来专门操作图片的API
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
//如果有一项为0 则表示一定不是正经的图片
if(height == 0 || width == 0){
return null;
}
//3.分目录存储文件 /yyyy/MM/dd
//3.1 准备文件根目录
String dateDir = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
//拼接文件目录 F:/images/2021/MM/dd/
String dirPath = localDir + dateDir;
File dirFile = new File(dirPath);
//3.2 判断是否需要创建目录
if(!dirFile.exists()){ //不存在目录时,应该创建目录
dirFile.mkdirs();
}
//4.防止文件重名 UUID.后缀
String uuid = UUID.randomUUID().toString().replace("-", "");
//获取.的下标位置
int index = fileName.lastIndexOf(".");
//截取文件类型
String fileType = fileName.substring(index);
//拼接新文件路径
String realFileName = uuid + fileType;
//5.实现文件上传操作
//5.1 准备文件的全路径 文件目录/文件名称
String realFilePath = dirPath + realFileName;
//5.2 实现文件上传
file.transferTo(new File(realFilePath));
//6.封装返回值结果
//封装虚拟路径 /2021/11/11/uuid.jpg
String virtualPath = dateDir + realFileName;
//封装URL地址 协议名称://域名:端口号/图片虚拟地址
String urlPath = preURLPath + virtualPath;
System.out.println("图片网络地址:"+urlPath);
//封装VO对象
ImageVO imageVO = new ImageVO();
imageVO.setVirtualPath(virtualPath);
imageVO.setUrlPath(urlPath);
imageVO.setFileName(realFileName);
return imageVO;
} catch (IOException e) {
e.printStackTrace();
return null; //如果程序执行报错,则返回null
}
}
@Override
public void deleteFile(String virtualPath) {
//1.准备文件的全路径 本地路径 + 虚拟地址
String path = localDir + virtualPath;
//2.将路径封装为对象
File file = new File(path);
//3.实现文件删除
file.delete();
}
}