You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
warehouse/src/main/java/com/yeqifu/sys/controller/FileController.java

107 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.yeqifu.sys.controller;
import cn.hutool.core.date.DateUtil;
import com.yeqifu.sys.common.AppFileUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.HttpStatus;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: 落亦-
* @Date: 2019/12/15 23:46
*/
@RestController
@RequestMapping("file")
public class FileController {
/**
* 图片上传
* @param mf
* @return
*/
@RequestMapping("uploadImage")
public Map<String, Object> uploadImage(MultipartFile mf) {
// 1. 得到文件名
String oldName = mf.getOriginalFilename();
// 2. 根据旧的文件名生成新的文件名
String newName = AppFileUtils.createNewFileName(oldName);
// 3. 得到当前日期的字符串
String dirName = DateUtil.format(new Date(), "yyyy-MM-dd");
// 4. 构造文件夹
File dirFile = new File(AppFileUtils.UPLOAD_PATH, dirName);
// 5. 判断当前文件夹是否存在
if (!dirFile.exists()) {
// 如果不存在则创建新文件夹
dirFile.mkdirs();
}
// 6. 构造文件对象
File file = new File(dirFile, newName + "_temp");
// 7. 把 mf 里面的图片信息写入 file
try {
mf.transferTo(file);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<>();
map.put("path", dirName + "/" + newName + "_temp");
return map;
}
/**
* 图片下载
* @param path 图片路径
* return
*/
@RequestMapping("showImageByPath")
public ResponseEntity<byte[]> downloadImage(String path) {
// 规范路径,防止路径穿越
Path normalizedPath = Paths.get(AppFileUtils.UPLOAD_PATH, path).normalize();
// 防止路径越界,确保文件在允许的上传目录下
if (!normalizedPath.startsWith(AppFileUtils.UPLOAD_PATH)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); // 如果路径越界返回403 Forbidden
}
// 检查文件是否存在
File file = normalizedPath.toFile();
if (!file.exists() || !file.isFile()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // 文件不存在返回404 Not Found
}
try {
// 读取文件内容
byte[] fileContent = Files.readAllBytes(normalizedPath);
// 根据文件扩展名推测文件类型并返回适当的 Content-Type
String contentType = Files.probeContentType(normalizedPath);
if (contentType == null) {
contentType = "application/octet-stream"; // 默认二进制流类型
}
// 返回文件内容,设置响应头和状态码
return ResponseEntity.ok()
.contentType(MediaType.valueOf(contentType))
.body(fileContent);
} catch (IOException e) {
// 异常捕获并返回500服务器错误
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); // 服务器错误
}
}
}