使用Java 操作MinIO,特殊渠道拿到阿里大厂面试真题

}

return items;

}

@PostMapping(“/upload”)

public Res upload(@RequestParam(name = “file”, required = false) MultipartFile[] file) {

Res res = new Res();

res.setCode(500);

if (file == null || file.length == 0) {

res.setMessage(“上传文件不能为空”);

return res;

}

List orgfileNameList = new ArrayList<>(file.length);

for (MultipartFile multipartFile : file) {

String orgfileName = multipartFile.getOriginalFilename();

orgfileNameList.add(orgfileName);

try {

InputStream in = multipartFile.getInputStream();

minioClient.putObject(MINIO_BUCKET, orgfileName, in, new PutObjectOptions(in.available(), -1));

in.close();

} catch (Exception e) {

log.error(e.getMessage());

res.setMessage(“上传失败”);

return res;

}

}

Map<String, Object> data = new HashMap<String, Object>();

data.put(“bucketName”, MINIO_BUCKET);

data.put(“fileName”, orgfileNameList);

res.setCode(200);

res.setMessage(“上传成功”);

res.setData(data);

return res;

}

@RequestMapping(“/download/{fileName}”)

public void download(HttpServletResponse response, @PathVariable(“fileName”) String fileName) {

InputStream in = null;

try {

ObjectStat stat = minioClient.statObject(MINIO_BUCKET, fileName);

response.setContentType(stat.contentType());

response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(fileName, “UTF-8”));

in = minioClient.getObject(MINIO_BUCKET, fileName);

IOUtils.copy(in, response.getOutputStream());

} catch (Exception e) {

log.error(e.getMessage());

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

log.error(e.getMessage());

}

}

}

}

@DeleteMapping(“/delete/{fileName}”)

public Res delete(@PathVariable(“fileName”) String fileName) {

Res res = new Res();

res.setCode(200);

try {

minioClient.removeObject(MINIO_BUCKET, fileName);

} catch (Exception e) {

res.setCode(500);

log.error(e.getMessage());

}

return res;

}

private static String formatFileSize(long fileS) {

DecimalFormat df = new DecimalFormat(“#.00”);

String fileSizeString = “”;

String wrongSize = “0B”;

if (fileS == 0) {

return wrongSize;

}

if (fileS < 1024) {

fileSizeString = df.format((double) fileS) + " B";

} else if (fileS < 1048576) {

fileSizeString = df.format((double) fileS / 1024) + " KB";

} else if (fileS < 1073741824) {

fileSizeString = df.format((double) fileS / 1048576) + " MB";

} else {

fileSizeString = df.format((double) fileS / 1073741824) + " GB";

}

return fileSizeString;

}

}

  • Res 文件

import lombok.AllArgsConstructor;

import lombok.NoArgsConstructor;

import java.io.Serializable;

@lombok.Data

@AllArgsConstructor

@NoArgsConstructor

public class Res implements Serializable {

private static final long serialVersionUID = 1L;

private Integer code;

private Object data = “”;

private String message = “”;

}

  • 路由文件 RouterController

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

@Controller

public class RouterController {

@GetMapping({“/”, “/index.html”})

public String index() {

return “index”;

}

@GetMapping({“/upload.html”})

public String upload() {

return “upload”;

}

}

  • 前端 列表页面 src\main\resources\templates\index.html
图片列表

上传图片


<el-table :data=“results” stripe style=“width: 60%” @row-click=“preview”>

下载

<a :href=“‘/delete/’ + scope.row.fileName + ‘’” @click.prevent=“deleteFile( e v e n t , s c o p e . event,scope. event,scope.index,results)”

class=“el-icon-delete”>删除


预览图片


  • 前端上传页面 src\main\resources\templates\upload.html
图片上传
选择图片

返回图片列表页面

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值