从minio中读取文件流进行下载文件

蔚落 2023-10-18 09:21 176阅读 0赞

一、获取Minio连接

  1. public static String minioUrl;
  2. public static String minioUsername;
  3. public static String minioPassword;
  4. @Value("${system.minioUrl}")
  5. private String minioUrlTmp;
  6. @Value("${system.minioUsername}")
  7. private String minioUsernameTmp;
  8. @Value("${system.minioPassword}")
  9. private String minioPasswordTmp;
  10. @PostConstruct
  11. public void init() {
  12. minioUrl = minioUrlTmp;
  13. minioUsername = minioUsernameTmp;
  14. minioPassword = minioPasswordTmp;
  15. }
  16. public static MinioClient getInstance() {
  17. if (minioClient == null) {
  18. synchronized (FileUtils.class) {
  19. if (minioClient == null) {
  20. try {
  21. //new FileUtils();
  22. minioClient = new MinioClient(minioUrl,minioUsername,minioPassword);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  28. }
  29. return minioClient;
  30. }

二、读取文件流

  1. InputStream in = null;
  2. OutputStream out = null;
  3. ExamineFileList examineFileList = examineFileListRepository.findOne(guid);
  4. MinioClient minioClient = FileUtils.getInstance();
  5. try {
  6. in = minioClient.getObject(examineFileList.getFileDirectory(), examineFileList.getFilePath()+examineFileList.getFileNameUuid());
  7. int len = 0;
  8. byte[] buffer = new byte[1024];
  9. out = response.getOutputStream();
  10. response.reset();
  11. response.addHeader("Content-Disposition",
  12. " attachment;filename=" + new String(examineFileList.getFileName().getBytes(),"iso-8859-1"));
  13. response.setContentType("application/octet-stream");
  14. while ((len = in.read(buffer)) > 0) {
  15. out.write(buffer, 0, len);
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. if (in != null){
  21. try {
  22. in.close();
  23. } catch (Exception e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. if (out != null) {
  28. try {
  29. out.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

发表评论

表情:
评论列表 (有 0 条评论,176人围观)

还没有评论,来说两句吧...

相关阅读