文件上傳
文件上傳到服務器指定目錄,記錄文件存放路徑,生成文件唯一名(日期+隨機Id)
文件預覽
方案1: 將可預覽的文件上傳到nginx 代理的靜態資源路徑,然後記錄該路徑地址,以後訪問預覽該文件的時候可以直接通過ngix代理進行直接預覽
方案2: 通過訪問的文件名,在數據庫獲取文件的全路徑,後臺將文件流的放到response
文件下載
同文件預覽方案2,在response相應中增加ContentType("application/octet-stream")標識
實現效果展示
上傳結果
文件數據記錄
圖片預覽
文本型文件預覽
不支持預覽的文件進行預覽
代碼實現
file.properties 文件上傳配置文件
@Component
@ConfigurationProperties(prefix = "file", ignoreUnknownFields = true, ignoreInvalidFields= true)
@PropertySource("classpath:file.properties")
@Data
public class FileConfig implements Serializable {
/**
* 文件上傳路徑
*/
private String uploadPath;
/**
* 可預覽的圖片類型
*/
private String previewImgType;
/**
* 可預覽的文本型類型
*/
private String previewTextType;
}
# 文件上傳路徑
file.uploadPath=E:\\fileUpload
# 可預覽的圖片類型
file.previewImgType=PNG,JPG,JPEG,BMP,GIF,SVG
# 可預覽的文本型類型
file.previewTextType=txt,htm,html,json,xml
file 的數據庫表
DROP TABLE IF EXISTS `file`;
CREATE TABLE `file` (
`id` varchar(36) NOT NULL,
`fileName` varchar(255) NOT NULL COMMENT '文件名',
`fileFullName` varchar(255) DEFAULT NULL COMMENT '文件全路徑',
`fileOriginalName` varchar(255) DEFAULT NULL COMMENT '文件原始名',
`fileSize` int(11) DEFAULT NULL COMMENT '文件大小',
`created_by` varchar(255) DEFAULT NULL COMMENT '創建人',
`created_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '創建時間',
`last_modified_by` varchar(255) DEFAULT NULL COMMENT '最後更新者',
`last_modified_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '最後更新時間',
`is_del` tinyint(2) DEFAULT '0' COMMENT '是否刪除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf32;
controller層提供上傳,預覽,下載三個api
@RequestMapping("/api")
@Api(value = "file")
@RestController
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@Autowired
private FileService fileService;
/**
* Post /file/upload : 文件上傳
*/
@PostMapping("/file/upload")
@ApiOperation(value="文件上傳接口", notes="文件上傳接口")
public ResponseEntity<ResponseResult> upload(HttpServletRequest request) throws Exception {
// 獲取請求頭的文件信息
MultipartHttpServletRequest multipartHttpServletRequest = null;
try {
multipartHttpServletRequest = (MultipartHttpServletRequest)request;
} catch (Exception e) {
log.error("獲取文件信息出現錯誤", e);
throw new BizLogicException(new SystemMessage("GET_FILE_ERROR","獲取文件信息出現錯誤"));
}
MultipartFile file = multipartHttpServletRequest.getFile("file");
String fileUrl = fileService.upload(file);
return new ResponseEntity<>(ResponseResult.success(fileUrl), HttpStatus.OK);
}
@GetMapping("/file/preview/{fileName}")
@ApiOperation(value="文件預覽接口", notes="文件預覽接口")
public void preview(@PathVariable @NotNull String fileName, HttpServletResponse response) throws Exception {
InputStream in = null;
ServletOutputStream sos = null;
File file = fileService.preview(fileName);
try {
in = new FileInputStream(file);// TODO 中文可能出現亂碼
sos = response.getOutputStream();
byte[] b = new byte[1024];
while (in.read(b) != -1) {
sos.write(b); //輸出
}
sos.flush(); //刷新
} catch (Exception ex) {
log.error("文件預覽失敗,異常信息=[{}]", ex);
ex.printStackTrace();
} finally {
try {
in.close(); //關閉文件讀取流,輸出流
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@GetMapping("/file/download/{fileName}")
@ApiOperation(value="文件下載", notes="文件下載")
public void download(@PathVariable @NotNull String fileName, HttpServletResponse response) throws Exception {
// 以流的形式下載文件。
FileInfo fileInfo = fileService.getFileInfo(fileName);
InputStream in = null;
OutputStream os = null;
try {
File file = new File(fileInfo.getFileName());
in = new BufferedInputStream(new FileInputStream(fileInfo.getFileFullName()));
byte[] buffer = new byte[in.available()];
in.read(buffer);
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
response.setCharacterEncoding("utf-8");
os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
os.flush();
} catch (Exception ex) {
log.error("文件下載失敗,異常信息=[{}]", ex);
ex.printStackTrace();
} finally {
in.close();
os.close();
}
}
}
@Service("FileService")
@Transactional(rollbackFor = Exception.class)
public class FileService {
private static final Logger log = LoggerFactory.getLogger(FileService.class);
@Autowired
private FileConfig fileConfig;
@Autowired
private FileInfoDao fileInfoDao;
/**
* 文件上傳
* @param file
* @return
* @throws Exception
*/
public String upload(MultipartFile file) throws Exception {
if (file.isEmpty()) {
throw new BizLogicException(new SystemMessage("FILE_NOT_EMPTY_ERROR","上傳文件不可爲空"));
}
// 生成文件Id
String fileId = UUID.randomUUID().toString();
// 獲得文件名
String fileOriginalName = file.getOriginalFilename();
log.info("獲取上傳的文件名=[{}]", fileOriginalName);
// 獲取文件後綴
String fileSuffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1);
// 生成文件名稱
String newFileName = MessageFormat.format("{0}{1}.{2}",
DateUtil.toString(DateUtil.getCurDate(),"yyyyMMddHHmmss"),
fileId.substring(0,5),
fileSuffix);
log.info("生成新的文件名稱=[{}]", newFileName);
// 0配置根路徑 + 1路徑分割符 + 2日期年月日 + 3路徑分割符
String fullFolderPath = MessageFormat.format("{0}{1}{2}",
fileConfig.getUploadPath(),
File.separator,
DateUtil.toString(DateUtil.getCurDate(),"yyyyMMdd"));
log.info("上傳文件的路徑=[{}]", fullFolderPath);
File fileUIS = new File(fullFolderPath);
if(!fileUIS.exists()){// 如果不存在文件夾創建文件夾,每天第一次上傳文件會創建yyyyMMdd的文件夾
fileUIS.mkdirs();
}
String fileFullName = MessageFormat.format("{0}{1}{2}",
fullFolderPath, File.separator, newFileName);
File fullPathFile = new File(fileFullName);
try {
file.transferTo(fullPathFile); //保存文件
} catch (IOException | IllegalStateException ex) {
log.error("文件=[{}] 上傳失敗,異常信息=[{}]", fileOriginalName, ex);
throw new BizLogicException(new SystemMessage("FILE_UPLOAD_ERROR","文件上傳失敗!"));
}
FileInfo fileInfo = new FileInfo();
fileInfo.setId(fileId);// 文件Id
fileInfo.setFileName(newFileName); // 文件名
fileInfo.setFileFullName(fileFullName);// 文件全路徑
fileInfo.setFileOriginalName(fileOriginalName);
fileInfo.setFileSize(file.getSize());
int insertResult = fileInfoDao.insert(fileInfo);
log.info("文件上傳數據記錄成功=[{}]", insertResult);
return newFileName;
}
/**
* 文件預覽
* @param fileName
* @return
* @throws Exception
*/
public File preview(String fileName) throws Exception {
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (Arrays.asList(StringUtils.split(fileConfig.getPreviewImgType().toLowerCase(),",")).contains(fileSuffix.toLowerCase())
|| Arrays.asList(StringUtils.split(fileConfig.getPreviewTextType().toLowerCase(),",")).contains(fileSuffix.toLowerCase())) {
// 可根據文件後綴不同,執行特殊處理
log.debug("該文件類型類型=[{}]支持預覽", fileSuffix);
} else {
log.error("該文件類型類型=[{}]暫不支持預覽", fileSuffix);
throw new BizLogicException(new SystemMessage("FILE_NOT_PREVIEW","該文件類型類型暫不支持預覽,請下載後查看!"));
}
String fullName = getFileInfo(fileName).getFileFullName();
File file = new File(fullName);
return file;
}
/**
* 通過fileName 獲取fileInfo信息
* @return
*/
public FileInfo getFileInfo(String fileName) throws Exception{
FileInfo fileInfo = fileInfoDao.findByFileName(fileName);
if (fileInfo == null || "".equals(fileInfo.getId())) {
throw new BizLogicException(new SystemMessage("FILE_NOT_FOUNT","文件信息沒有找到,無法預覽!"));
}
return fileInfo;
}
}
dao層findByFileName 方法不能像hibatenate 直接聲明使用,需要些對應的mapper 查詢
/**
* 文件數據操作類
*/
@Mapper
public interface FileInfoDao extends BaseMapper<FileInfo> {
FileInfo findByFileName(String fileName);
}
數據的增刪改查基於MyBatis 通用 Mapper,pom依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
在resources下面新建static文件夾,創建upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8089/api/file/upload"
enctype="multipart/form-data" method="POST" >
上傳文件:<input type="file" name="file"/><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
代碼示例源碼放在了
https://github.com/yenrocProject/blog-system/tree/develop-file-version0.1/src/main/resources