作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 BlobstoreInputStream 读取超过 1mb 的数据将引发 IOException“Blob fetch size too large”。
您可以使用以下答案中的 ChainedBlobstoreInputStream 类来解决此问题。
最佳答案
我创建了一个简单的包装类来解决这个问题。您应该能够直接将 ChainedBlobstoreInputStream 交换为 BlobstoreInputStream。我还没有测试过 mark()、markSupported() 或 reset() 方法。
您可以随意使用此代码。
package net.magicscroll.server.blobstore;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* ChainedBlobstoreInputStream works exactly like BlobstoreInputStream but does
* not throw an error if more than 1mb is read.
*
* @author Richard Wallis
*/
public class ChainedBlobstoreInputStream extends InputStream {
/**
* The maximum number of bytes that can be read by a single request.
*/
private static final int MAX_READSIZE = 1015800;
/**
* The BlobKey of the blobstore item.
*/
private BlobKey blobKey;
/**
* The current byte position of the reader.
*/
private long offset;
/**
* The Total Size of the blob.
*/
private long totalSize;
/**
* The current Input Stream being read.
*/
private BlobstoreInputStream currentStream;
/**
* The next point at which a new InputStream will need to be initialized.
*/
private long nextReadBreak;
/**
* The currentStream at the time of the last mark.
*/
private BlobstoreInputStream markedStream;
/**
* Creates a new ChainedBlobstoreInputStream. This stream should behave
* exactly the same as a BlobstoreInputStream and it should be possible to
* interchange them.
*
* @param theBlobKey
* - The blobkey of the object to be read.
* @throws IOException
* - Thrown if there is an error reading the current stream.
*/
public ChainedBlobstoreInputStream(final BlobKey theBlobKey)
throws IOException {
this(theBlobKey, 0);
}
/**
* Creates a new ChainedBlobstoreInputStream. This stream should behave
* exactly the same as a BlobstoreInputStream and it should be possible to
* interchange them.
*
* @param theBlobKey
* - The blobkey of the object to be read.
* @param newOffset
* - The offset in the blob from where to read.
* @throws IOException
* - Thrown if there is an error reading the current stream.
*/
public ChainedBlobstoreInputStream(final BlobKey theBlobKey,
final long newOffset) throws IOException {
this.offset = newOffset;
this.blobKey = theBlobKey;
final BlobInfo blobInfo =
new BlobInfoFactory().loadBlobInfo(this.blobKey);
this.totalSize = blobInfo.getSize();
this.currentStream =
new BlobstoreInputStream(this.blobKey, this.offset);
this.nextReadBreak =
this.offset + ChainedBlobstoreInputStream.MAX_READSIZE;
}
/*
* (non-Javadoc)
* @see java.io.InputStream#read()
*/
@Override
public final int read() throws IOException {
if (this.offset < this.totalSize) {
if (this.offset == this.nextReadBreak) {
this.currentStream.close();
this.currentStream =
new BlobstoreInputStream(this.blobKey, this.offset);
this.nextReadBreak = this.offset
+ ChainedBlobstoreInputStream.MAX_READSIZE;
}
this.offset += 1;
return this.currentStream.read();
} else {
this.currentStream.close();
return -1;
}
}
/*
* (non-Javadoc)
* @see java.io.InputStream#close()
*/
@Override
public final void close() throws IOException {
this.currentStream.close();
super.close();
}
/*
* (non-Javadoc)
* @see java.io.InputStream#mark(int)
*/
@Override
public final void mark(final int readlimit) {
this.currentStream.mark(readlimit);
this.markedStream = this.currentStream;
}
/*
* (non-Javadoc)
* @see java.io.InputStream#markSupported()
*/
@Override
public final boolean markSupported() {
return this.currentStream.markSupported();
}
/*
* (non-Javadoc)
* @see java.io.InputStream#reset()
*/
@Override
public final void reset() throws IOException {
this.currentStream = this.markedStream;
this.currentStream.reset();
}
}
关于java - 读取大小 >= 1MB 的 BlobstoreInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410886/
从 BlobstoreInputStream 读取超过 1mb 的数据将引发 IOException“Blob fetch size too large”。 您可以使用以下答案中的 ChainedBl
我真的希望你能帮助我! 目标是能够通过我的(java - google app engine)应用程序上传数据文件(.tsv)并获取其内容并将其写入 mysql 数据库。 经过一番研究,blobsto
我是一名优秀的程序员,十分优秀!