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

本文转自:https://blog.csdn.net/ZHANGLIZENG/article/details/82892678

一、获取Minio连接
    public static String minioUrl;    
    public static String minioUsername; 
    public static String minioPassword; 
    @Value("${system.minioUrl}")  
    private String minioUrlTmp;
    
    @Value("${system.minioUsername}")  
    private String minioUsernameTmp;
    
    @Value("${system.minioPassword}")  
    private String minioPasswordTmp;

    @PostConstruct  
    public void init() {  
        minioUrl = minioUrlTmp;  
        minioUsername = minioUsernameTmp;
        minioPassword = minioPasswordTmp;
    }  
    
    
    
    public static MinioClient getInstance() {
        if (minioClient == null) {  
            synchronized (FileUtils.class) {  
                if (minioClient == null) {  
                    try {
                        //new FileUtils(); 
                        minioClient = new MinioClient(minioUrl,minioUsername,minioPassword);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }  
            }  
        }  
        return minioClient;  
    }  

二、读取文件流

        InputStream in = null;
        OutputStream out = null;
        ExamineFileList examineFileList = examineFileListRepository.findOne(guid);
        MinioClient minioClient = FileUtils.getInstance();
        try {
            in = minioClient.getObject(examineFileList.getFileDirectory(), examineFileList.getFilePath()+examineFileList.getFileNameUuid());
            int len = 0;
            byte[] buffer = new byte[1024];
            out = response.getOutputStream();
            response.reset();
            response.addHeader("Content-Disposition",    
                    " attachment;filename=" + new String(examineFileList.getFileName().getBytes(),"iso-8859-1"));
            response.setContentType("application/octet-stream");
            while ((len = in.read(buffer)) > 0) {
               out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null){
               try {
                  in.close();
               } catch (Exception e) {
                  throw new RuntimeException(e);
               }
            }
            if (out != null) {
               try {
                   out.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
            }
        }
---------------------
作者:ZHANGLIZENG
来源:CSDN
原文:https://blog.csdn.net/ZHANGLIZENG/article/details/82892678
版权声明:本文为博主原创文章,转载请附上博文链接!

 

posted on 2019-01-24 22:54  freeliver54  阅读(10999)  评论(1编辑  收藏  举报

导航