- 测试上传到Aliyun
- OSSClient单例是否需要关闭?
- 字段设计
/** id */
private String up2ossId;
/** 业务id(必须全局唯一) */
private String businessId;
/** 工程名称 */
private String project;
/** 模块名称 */
private String module;
/** 文件名 */
private String filename;
/** project/module/yyyy/mm/filename.ext */
private String objectName;
/** 文件扩展名 */
private String ext;
/** 大小 */
private String size;
/** 删除标记: 0 =valid, 1=deleted */
private Integer isDeleted;
/** 创建日期 */
private Date createTime;
/** 创建人 */
private String createBy;
/** 文件流 */
private Resource resource;
- 需提供的API
- upload(MultipartFile, Attachment)
- download(id):ResponseEntity<Resource>
- delById(id)
- listAttachmentVOByBusinessId(businessId)
AttachmentVO{uid,url,filename}
ever code abstract
# 上传controller
@PostMapping
@ResponseBody
public Result uploadFile(@RequestParam("file") MultipartFile file,Attachment attachment){
//1 上传
//2 返回busiId对应的多个已上传列表
}
# service
Files.copy(file.getInputStream(), Paths.get(rootLocation,attachPath));
# 根据controller方法动态生成url - MvcUriComponentsBuilder
fileListVO.setUrl(MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class, "serveFile", attachment.getId())
.build().toString());
# 下载Controller
@GetMapping("/files/{id}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String id) {
String filename = new String(attachment.getAttachName().getBytes("gbk"), "iso8859-1");
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE,"octets/stream")
.header(HttpHeaders.ACCEPT_CHARSET, "utf-8")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+filename+"\"")
.body(file);
# 下载流生成 - UrlResource
Path file = Paths.get(rootLocation,attachment.getAttachPath());
Resource resource = new UrlResource(file.toUri());
if(resource.exists() || resource.isReadable()) {
attachment.setResource(resource);
}
网友评论