1 .文件上传
文件上传的业务接口文档
- 请求路径: http://localhost:8091/file/upload
- 请求类型: post
- 请求参数:
参数名称 | 参数说明 | 备注 |
---|
file | 文件上传的参数名称 | file中携带的是二进制信息 |
参数名称 | 参数说明 | 备注 |
---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 返回ImageVO对象 |
参数名称 | 参数类型 | 参数说明 | 备注 |
---|
virtualPath | String | 图片实际路径 不包含磁盘信息 | 例如: 2021/11/11/a.jpg 不需要写磁盘地址 |
urlPath | String | 图片url访问地址 | http://image.jt.com/2021/11/11/a.jpg 需要指定域名地址 |
fileName | String | 文件上传后的文件名称 | UUID.type |
1.1 编辑ImageVO
@Data
@Accessors(chain = true)
public class ImageVO {
private String virtualPath;
private String urlPath;
private String fileName;
}
1.2 编辑FileController
@PostMapping("/upload")
public SysResult upload(MultipartFile file) throws IOException {
ImageVO imageVO = fileService.upload(file);
if(imageVO == null){
return SysResult.fail();
}
return SysResult.success(imageVO);
}
1.3 编辑FileServiceImpl
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{
private String localDir = "F:/images";
private String preURLPath = "http://image.jt.com";
@Override
public ImageVO upload(MultipartFile file) {
String fileName = file.getOriginalFilename();
fileName = fileName.toLowerCase();
if(!fileName.matches("^.+\\.(jpg|png|gif)$")){
return null;
}
try {
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
if(height == 0 || width == 0){
return null;
}
String dateDir = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
String dirPath = localDir + dateDir;
File dirFile = new File(dirPath);
if(!dirFile.exists()){
dirFile.mkdirs();
}
String uuid = UUID.randomUUID().toString().replace("-", "");
int index = fileName.lastIndexOf(".");
String fileType = fileName.substring(index);
String realFileName = uuid + fileType;
String realFilePath = dirPath + realFileName;
file.transferTo(new File(realFilePath));
String virtualPath = dateDir + realFileName;
String urlPath = preURLPath + virtualPath;
System.out.println("图片网络地址:"+urlPath);
ImageVO imageVO = new ImageVO();
imageVO.setVirtualPath(virtualPath);
imageVO.setUrlPath(urlPath);
imageVO.setFileName(realFileName);
return imageVO;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
2.文件删除操作
2.1 文件删除业务接口
- 请求路径: http://localhost:8091/file/deleteFile
- 请求类型: delete
- 请求参数:
参数名称 | 参数说明 | 备注 |
---|
virtualPath | 文件上传的虚拟的路径 | 删除时需要磁盘路径一起删除 |
参数名称 | 参数说明 | 备注 |
---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 可以为null |
2.2 编辑FileController
@DeleteMapping("/deleteFile")
public SysResult deleteFile(String virtualPath){
fileService.deleteFile(virtualPath);
return SysResult.success();
}
2.3 编辑FileServiceImpl
@Override
public void deleteFile(String virtualPath) {
String path = localDir + virtualPath;
File file = new File(path);
file.delete();
}