概述过程
首先项目框架中配好miniio
调用miniio方法,传正确参数
方法
上传
/**
* 上传文件
* 文件名采用uuid,避免原始文件名中带"/"符号导致下载的时候解析出现异常
*
* @param file 资源
* @param dir 文件夹 (前后不带/)
* @param rename 是否重命名 默认true
* @return R(bucketName, filename, fullName)
*/
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Resp<FileStatVo> upload(@RequestPart("file") MultipartFile file,
@RequestParam(required = false) String dir,
@RequestParam(required = false, defaultValue = "true") boolean rename) {
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
if (rename) {
fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
}
String fullName = BucketConstants.BUCKET + StrUtil.SLASH + fileName;
String uploadPath = fileName;
if (StrUtil.isNotBlank(dir)) {
uploadPath = dir + StrUtil.SLASH + fileName;
fullName = BucketConstants.BUCKET + StrUtil.SLASH + uploadPath;
}
FileStatVo stat = new FileStatVo();
stat.setBucketName(BucketConstants.BUCKET);
stat.setFileName(fileName);
stat.setFullName(fullName);
try {
// minioTemplate.putObject(BucketConstants.BUCKET, uploadPath, file.getInputStream());
minioTemplate.putObject(BucketConstants.BUCKET, uploadPath, file.getInputStream(),contentType);
} catch (Exception e) {
log.error("上传失败:{}", e.getMessage(), e);
return Resp.failed("上传失败:" + e.getLocalizedMessage());
}
return Resp.ok(stat);
}
/**
* 上传文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param stream 文件流
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, String conType) throws Exception {
// this.putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
this.putObject(bucketName, objectName, stream, stream.available(), conType);
}
下载
/**
* 下载文件
*
* @param file 文件路径
*/
@GetMapping("/download")
public void download(@RequestParam String file, HttpServletResponse response) {
String bucket = org.apache.commons.lang3.StringUtils.substringBefore(file, StrUtil.SLASH);
String filePath = org.apache.commons.lang3.StringUtils.substringAfter(file, StrUtil.SLASH);
String fileName = org.apache.commons.lang3.StringUtils.substringAfterLast(file, StrUtil.SLASH);
try (InputStream inputStream = minioTemplate.getObject(bucket, filePath)) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setCharacterEncoding("UTF-8");
IoUtil.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
log.error("读取异常:{}", file, e);
}
}
预览
/**
* 在线预览文件
*
* @param file 文件路径
*/
@GetMapping("/view")
public void file(@RequestParam String file, HttpServletResponse response) {
String bucket = StringUtils.substringBefore(file, StrUtil.SLASH);
String filePath = StringUtils.substringAfter(file, StrUtil.SLASH);
String fileName = org.apache.commons.lang3.StringUtils.substringAfterLast(file, StrUtil.SLASH);
try (InputStream inputStream = minioTemplate.getObject(bucket, filePath)) {
ObjectStat stat = minioTemplate.getObjectInfo(bucket, filePath);
response.setContentType(stat.contentType());
// response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(fileName, "UTF-8"));
IoUtil.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
log.error("读取异常:{}", file, e);
}
}
预览页面–easyui
view: function (filePath) {
console.log("showfilepath:"+filePath);
if(filePath.indexOf(".jpg") >= 0 || filePath.indexOf(".JPG") >= 0 || filePath.indexOf(".GIF") >= 0 || filePath.indexOf(".gif") >= 0 || filePath.indexOf(".png") >= 0 || filePath.indexOf(".PNG") >= 0){
$('<img>', {
src: ctx + "/api/fs/view?file=" + filePath
}).viewer('show');
console.log("img:"+ctx + "/api/fs/view?file=" + filePath);
} else {
var url = ctx + "/api/fs/view?file=" + filePath;
console.log("pdf:"+url);
window.open(url,'_blank');//打开另一个窗口
// let mywin = window.open('','_self');//本页打开
// mywin.location.href = url;
}
},