【问题标题】:GridFSDownloadStream can not read all dataGridFSDownloadStream 无法读取所有数据
【发布时间】:2016-07-10 13:37:57
【问题描述】:

我尝试从 mongodb 下载文件。上传文件的大小是2106字节,chunkSize=2048字节,所以GridFS把文件分成2块。当我执行downloadStream.read(bytesToWriteTo)的代码时,只能读取第一个块的数据,不能读取第二个块的数据。如何读取所有数据?

public class OpenDownloadStreamDemo {

          public static void main(String[] args) {
            MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
            MongoDatabase mongoDatabase = mongoClient.getDatabase("demo");
            GridFSBucket gridFSBucket = GridFSBuckets[enter image description here][1].create(mongoDatabase);
            ObjectId fileId = new ObjectId("56f25a8b163b4598987b666b");
            GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
            int fileLength = (int) downloadStream.getGridFSFile().getLength();
            byte[] bytesToWriteTo = new byte[fileLength];
            downloadStream.read(bytesToWriteTo);
            downloadStream.close();
            System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
        }
    }

The files Collection

The chunks Collection

【问题讨论】:

    标签: java mongodb gridfs


    【解决方案1】:

    这让我困惑了一天,GridFS 页面上的示例只调用了一次读取,但为了下载所有块,您需要重复调​​用它。每次调用都会返回从当前块读取的字节数,因此您必须将这些值相加以跟踪当前位置并将该值传递回 read(...) 以便将卡盘复制到正确的在您的字节数组中的位置。我就是这样实现的。

    public class OpenDownloadStreamDemo {
    
          public static void main(String[] args) {
            MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
            MongoDatabase mongoDatabase = mongoClient.getDatabase("demo");
            GridFSBucket gridFSBucket = GridFSBuckets[enter image description here][1].create(mongoDatabase);
            ObjectId fileId = new ObjectId("56f25a8b163b4598987b666b");
            GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
            int fileLength = (int) downloadStream.getGridFSFile().getLength();
            byte[] bytesToWriteTo = new byte[fileLength];
            int streamDownloadPosition = 0;
            while(streamDownloadPosition != -1) {
                streamDownloadPosition += downloadStream.read(bytesToWriteTo, streamDownloadPosition, fileLength);
            }
            downloadStream.close();
            System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以直接使用输入流。打开的下载流足以获取所有的chunk

      GridFSDownloadStream downloadStream = mongo.getGridFSBucket().openDownloadStream(fileId);    
      //int fileLength = (int) downloadStream.getGridFSFile().getLength();    
      //byte[] bytesToWriteTo = new byte[(fileLength];    
      //downloadStream.read(bytesToWriteTo);    
      return downloadStream;   
      

      【讨论】:

        【解决方案3】:

        我知道这是一个很晚的回复,您可以尝试使用以下实现

        public static void main(String[] args) {
                MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
                MongoDatabase mongoDatabase = mongoClient.getDatabase("demo");
                GridFSBucket gridFSBucket = GridFSBuckets.create(mongoDatabase);
                ObjectId fileId = new ObjectId("56f25a8b163b4598987b666b");
                GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                int data = downloadStream.read();
                while (data >= 0) {
                    outputStream.write((char) data);
                    data = downloadStream.read();
                }
                byte[] bytesToWriteTo = outputStream.toByteArray();
                downloadStream.close();
                System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          • 1970-01-01
          • 2012-11-18
          • 2019-07-26
          • 2021-08-12
          相关资源
          最近更新 更多